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

Associaction.Find when associations is NULL #7374

Open
bkmeneguello opened this issue Feb 18, 2025 · 0 comments
Open

Associaction.Find when associations is NULL #7374

bkmeneguello opened this issue Feb 18, 2025 · 0 comments
Labels
type:question general questions

Comments

@bkmeneguello
Copy link

Discussed in #5649

Originally posted by othane August 25, 2022
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:

  1. 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?
  2. 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?

Thanks for any help with this !!

@github-actions github-actions bot added the type:question general questions label Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

1 participant