Skip to content

feat: support embedded preload#6137

Merged
jinzhu merged 6 commits intogo-gorm:masterfrom
black-06:embedded_preload
Apr 11, 2023
Merged

feat: support embedded preload#6137
jinzhu merged 6 commits intogo-gorm:masterfrom
black-06:embedded_preload

Conversation

@black-06
Copy link
Copy Markdown
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.

2 participants