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

Preloading with foreignKey and references overwrite does not work #6750

Open
cstaud opened this issue Dec 15, 2023 · 1 comment
Open

Preloading with foreignKey and references overwrite does not work #6750

cstaud opened this issue Dec 15, 2023 · 1 comment
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@cstaud
Copy link

cstaud commented Dec 15, 2023

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.

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"`
}
@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Dec 15, 2023
@a631807682
Copy link
Member

#5793 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

3 participants