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

feat: support embedded preload #6137

Merged
merged 6 commits into from Apr 11, 2023
Merged

Conversation

black-06
Copy link
Contributor

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Support embedded preload, especially the same structure. see #6131

Unimplemented:

  • Polymorphism has_one/has_many in repeating embedded struct // Their polymorphic value in db are all the same.

User Case Description

type Country struct {
	ID   int `gorm:"primaryKey"`
	Name string
}
type Address struct {
	CountryID int
	Country   Country
}
type NestedAddress struct {
	Address
}
type Org struct {
	ID              int
	PostalAddress   Address `gorm:"embedded;embeddedPrefix:postal_address_"`
	VisitingAddress Address `gorm:"embedded;embeddedPrefix:visiting_address_"`
	AddressID       int
	Address         struct {
		ID int
		Address
	}
	NestedAddress *NestedAddress `gorm:"embedded;embeddedPrefix:nested_address_"`
}

// Only preload Org.Address and Org.Address.Country
db.Preload("Address.Country")  // "Address" is has_one, "Country" is belongs_to (nested relation)

// Only preload Org.VisitingAddress
db.Preload("PostalAddress.Country") // "PostalAddress.Country" is belongs_to (embedded relation)

// Only preload Org.NestedAddress
db.Preload("NestedAddress.Address.Country") // "NestedAddress.Address.Country" is belongs_to (embedded relation)

// All preloaded include "Address" but exclude "Address.Country", because it won’t preload nested associations.
db.Preload(clause.Associations)

@black-06 black-06 marked this pull request as draft March 10, 2023 10:26
@black-06 black-06 marked this pull request as ready for review March 10, 2023 12:35
@jinzhu jinzhu merged commit 828e22b into go-gorm:master Apr 11, 2023
@yetone yetone mentioned this pull request Apr 8, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants