go-gorm / playground

GORM Playground (Please Create PR for this project to report issues)
MIT License
89 stars 678 forks source link

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

Open cstaud opened 8 months ago

cstaud commented 8 months ago

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: