Consider I have BaseNotification
and ExtendedNotification
structs.
For some notifications they use base_notificaion
table only yet some of them will save some extra data into extended_notificaion
.
How to setup struct tag in these two structs so that when I run
db.Create(&entendedNotification) // or something else
GORM will know it need to insert a new tuple into extended_notification
if needed(for example either it is not nil
or fields are filled)
And when I call
db.Preload(`ExtendedNotification`).Find(¬ifications)
it will grab all necessary entities accordingly?
If you ensure A's primary key id is a foreign key in B, use like this
type A struct{
Id int `gorm:"column:id"`
B []B `gorm:"column:b;ForeignKey:Aid"`
}
type B struct{
Aid int
}
if not, I suggest using
func (a *A) AfterFind()error{
return db.Model(&B{}).Find(&a.B).Error
}