You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I have an account table, it has an optional 1 to 1 mapping to a subscription table via fk_account_id on the subscription. Something like this:
type Account struct {
ID uint `json:"-" gorm:"primarykey"`
Subscription *Subscription `json:",omitempty"`
}
type Subscription struct {
Status string
AccountID int `json:"-"`
Account *Account `json:",omitempty"`
}
If I try to load these nested relationship I can do it either using the Preload/Join initially or lazy load them later if they are missing. Something like this:
var account Account
err = a.GormDB.
Joins("Subscription", a.GormDB.Where(models.Subscription{Status: "active"})). // Preload works here too
Where(models.Account{ID: id}).
First(&account).
Error
if err != nil {
log.Println(err)
return
}
later in the code if I need the subscription I test if it was loaded and if not I lazy load it
// Subscription probably not preloaded, lazy loading them now
err = a.GormDB.Model(&acc).
Where("status in ?", []string{"active"}).
Association("Subscription").
Find(&acc.Subscription)
if err != nil {
log.Printf("Error loading subscription")
return err
}
Assuming there is a subscription associated with the account this all works great. But if there is no subscription related to the account rather than the account.Subscription == nil being true it is a struct with an empty value which can be hard to tell a part form an empty row in the table.
Worse still when I call the Association("Subscription").Find(&acc.Subscription) above to lazy load it (this could happen because I did not join it originally, or if the Join would set the Subscription to nil like I was expecting) ... But this still just sets the acc.Subscription to be a empty struct, not nil and not returning a gorm.ErrRecordNotFound error.
So my 2 questions are:
How to tell if an association is invalid, ie the reverse look up for a subscription above does not exist either when lazy loading or when using the join/preload?
How should you detect later if an association is not yet loaded so you can lazy load when your in a another function the requires it?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, I have an account table, it has an optional 1 to 1 mapping to a subscription table via fk_account_id on the subscription. Something like this:
If I try to load these nested relationship I can do it either using the Preload/Join initially or lazy load them later if they are missing. Something like this:
later in the code if I need the subscription I test if it was loaded and if not I lazy load it
Assuming there is a subscription associated with the account this all works great. But if there is no subscription related to the account rather than the
account.Subscription == nil
being true it is a struct with an empty value which can be hard to tell a part form an empty row in the table.Worse still when I call the
Association("Subscription").Find(&acc.Subscription)
above to lazy load it (this could happen because I did not join it originally, or if the Join would set the Subscription to nil like I was expecting) ... But this still just sets theacc.Subscription
to be a empty struct, notnil
and not returning agorm.ErrRecordNotFound
error.So my 2 questions are:
Thanks for any help with this !!
Beta Was this translation helpful? Give feedback.
All reactions