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
When using a belongs-to relation with foreignKey and references overwrites preloading does not work.
Without the overwrite it works as expected.
type Country1 struct {
Name string `gorm:"primaryKey"`
}
type Country2 struct {
CName string `gorm:"primaryKey"`
}
type Address1 struct {
CountryName string
Country Country1
}
type Address2 struct {
CName string
Country Country2 `gorm:"foreignKey:CName;references:CName"`
}
type Org struct {
ID int
Adress1_1 Address1 `gorm:"embedded;embeddedPrefix:address1_1_"`
Adress1_2 Address1 `gorm:"embedded;embeddedPrefix:address1_2_"`
Adress2_1 Address2 `gorm:"embedded;embeddedPrefix:address2_1_"`
Adress2_2 Address2 `gorm:"embedded;embeddedPrefix:address2_2_"`
}
In the example above Address1 does not overwrite foreignKey nor references, and preloading works as expected.
Address2 overwrites foreignKey and references, preloading does not work.
c1_1 := Country1{Name: "c1_1"}
c1_2 := Country1{Name: "c1_2"}
c2_1 := Country2{CName: "c2_1"}
c2_2 := Country2{CName: "c2_2"}
org := Org{
Adress1_1: Address1{Country: c1_1},
Adress1_2: Address1{Country: c1_2},
Adress2_1: Address2{Country: c2_1},
Adress2_2: Address2{Country: c2_2},
}
DB.Create(&org)
var result Org
DB.Preload(clause.Associations).First(&result)
if result.Adress1_1.Country.Name != "c1_1" {
t.Error("Adress1_1 country has not been resolved")
}
if result.Adress1_2.Country.Name != "c1_2" {
t.Error("Adress1_2 country has not been resolved")
}
if result.Adress2_1.Country.CName != "c2_1" {
t.Error("Adress2_1 country has not been resolved")
}
if result.Adress2_2.Country.CName != "c2_2" {
t.Error("Adress2_2 country has not been resolved")
}
Fails with: Adress2_1 country has not been resolved && Adress2_2 country has not been resolved
Hint:
Only overwriting the reference and auto detecting the foreignKey works as expected.
Example:
type Address3 struct {
CountryID string
Country Country2 `gorm:"references:CName"`
}
The text was updated successfully, but these errors were encountered:
GORM Playground Link
go-gorm/playground#671
Description
When using a belongs-to relation with foreignKey and references overwrites preloading does not work.
Without the overwrite it works as expected.
In the example above Address1 does not overwrite foreignKey nor references, and preloading works as expected.
Address2 overwrites foreignKey and references, preloading does not work.
Fails with:
Adress2_1 country has not been resolved
&&Adress2_2 country has not been resolved
Hint:
Only overwriting the reference and auto detecting the foreignKey works as expected.
Example:
The text was updated successfully, but these errors were encountered: