Lets say i have these models
type BaseModel struct {
Id int `json: "id"`
CreatedAt time.Time `json:"created_at" sql:"type:timestamptz, default:now()"`
UpdatedAt time.Time `json:"updated_at" sql:"type:timestamptz"`
}
``golang
type Post struct {
BaseModelpg:"override"
AuthorID intjson:"author_id"
Author *Userjson:"author"
Vote intjson:"vote" sql:"default:0"
Body stringjson:"body" sql:"type:text"`
}
```golang
type Question struct {
Post `pg:"override"`
Title string `json:"title"`
Comments []Comment `json:"comments" pg:"many2many:comments_questions"`
}
``golang
type Comment struct {
Postpg:"override"`
}
Now for example if i want to find a question by id i should do something like following:
```golang
q := &Question{
Post: Post{
BaseModel: BaseModel{
Id: 2,
},
},
}
err := db.Select(q)
...
But this initialization is somehow ugly to me!
I know i can ignore inheritance and everything goes fine but is there any way to improve it or any better idea?
Thanks
I don't use db.Select and prefer more explicit version:
q := new(Question)
err := db.Model(q).Where("id = ?", 2).Select()
Other than that I don't have anything to add here - I don't use base models. Perhaps you should not too.
Thanks.
I don't use base models. Perhaps you should not too.
You mean that it's fine don't use only the base model or ignore all the inheritance for models and declare them on their own ?
I mean to get rid of BaseModel from your example and copy/paste columns where you need them.