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

wrong foreignKey setup for multiple ebedded belongs-to relations #672

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cstaud
Copy link

@cstaud cstaud commented Dec 15, 2023

Explain your user case and expected results

When using multiple embedded belongs-to relations only 1 database foreignKey is created.
Expected: a foreignKey for each relation is created.

type Country struct {
	Name string `gorm:"primaryKey"`
}

type Address struct {
	CountryName string
	Country     Country
}

type Org struct {
	ID       int
	Address1 Address `gorm:"embedded;embeddedPrefix:address1_"`
	Address2 Address `gorm:"embedded;embeddedPrefix:address2_"`
}

In the above example there should be a foreignKey for address1 -> countries and a foreignKey address2 -> countries.
gorm only creates one foreignKey fk_orgs_country => address2_country_name->countries

Hint:

  • the generated naming for the foreignKey should respect the fieldName with its path / parent struct, as both relations result in the same foreignKey name.
  • the relations are correctly detected as embedded and belongs-to, but result in only one entry in the relations map, this is due to the fact that only fieldName is used within this map
  • possible solution: consider using BindNames of the field for name generation

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.

1 participant