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

[Question] Bad examples about nested struct #772

Open
lijinliangyihao opened this issue Jun 27, 2024 · 1 comment
Open

[Question] Bad examples about nested struct #772

lijinliangyihao opened this issue Jun 27, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@lijinliangyihao
Copy link
Contributor

Document Link

https://gorm.io/docs/preload.html#Embedded-Preloading

Your Question

The examples about nested struct are very confusing and have some errors.

the doc's here:


type Address struct {  
  CountryID 
  int  Country   
  Country
}
type Org struct {  
  PostalAddress   Address `gorm:"embedded;embeddedPrefix:postal_address_"`  
  VisitingAddress Address `gorm:"embedded;embeddedPrefix:visiting_address_"`  
  Address         struct {    
    ID int    
    Address  
  }
}
// Only preload Org.Address and Org.Address.Country
db.Preload("Address.Country")  // "Address" is has_one, "Country" is belongs_to (nested association)

>>> Comment is wrong, should be PostalAddress
// Only preload Org.VisitingAddress
db.Preload("PostalAddress.Country") // "PostalAddress.Country" is belongs_to (embedded association)

>>> Comment is wrong, where's the NestedAddress?
// Only preload Org.NestedAddress
db.Preload("NestedAddress.Address.Country") // "NestedAddress.Address.Country" is belongs_to (embedded association)
// All preloaded include "Address" but exclude "Address.Country", because it won't preload nested associations.db.Preload(clause.Associations)

If no "embedded" tag on field "Org.Address",the generated sql‘s table name will be empty string, please give a more detailed example.

Expected answer

@a631807682 a631807682 added bug Something isn't working and removed type:question labels Jun 28, 2024
@a631807682
Copy link
Member

Can you create a PR for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants