Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to represent ForeignKey fields in gengeric model #3

Open
lunemec opened this issue Mar 1, 2018 · 1 comment
Open

How to represent ForeignKey fields in gengeric model #3

lunemec opened this issue Mar 1, 2018 · 1 comment

Comments

@lunemec
Copy link
Owner

lunemec commented Mar 1, 2018

Suppose you have MySQL model:

type Organization struct {
	ID         uint          `db:"id"`
	ParentID   sql.NullInt64 `db:"parent_id"`
	Name       string
	LocationID sql.NullInt64 `db:"location_id"`
}

which converts to "generic" app model:

type Organization struct {
	ID       uint
	Parent   *Organization
	Name     string
	Location *Location

	Contacts []Contact
}

Should generic app model have LocationID and ParentID fields? If so, why?
Wouldn't this be better?

func doSomething() {
	org := Organization{} // Loaded data from MySQL
	if org.Location != nil {  // <-- this seems clearer to mean Location can be empty.
		locationID := org.Location.ID
	}
}

// VS
func doSomethingElse() {
	org := Organization{} // Loaded data from MySQL
	if org.LocationID != 0 {
		locationID := org.LocationID
	}
}
@thfre
Copy link

thfre commented Mar 1, 2018

I don't think Generic model should have LocationID and ParentID. If the objects a nil then they have no relevance and if they are not, the objects themselves hold their keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants