Your Question
Gorm will not create foreign key if the relation field is embedded
type TestA struct {
ID uint `gorm:"primaryKey"`
Name string
//Bs []TestB `gorm:"foreignKey:TestAID;references:ID"`
C TestC `gorm:"embedded"`
}
type TestC struct {
Bs []TestB `gorm:"foreignKey:TestAID;references:ID"`
}
type TestB struct {
ID uint `gorm:"primaryKey"`
TestAID uint `gorm:"foreignKey:TestAID;references:ID"`
Weight int
}
And because of this, gorm won't add a relationship to TestB in this case:
https://github.com/go-gorm/gorm/blob/9d82aa56734999bb28e0c4d60fba69ae7cde66d5/schema/relationship.go#L95-L96
Why we need this special judge?
PS: i noticed that someone has mentioned this problem before #5816
The document you expected this should be explained
Expected answer
How to create foreign key for an embedded struct? Or Is there any other methods to create a foreign key?
Comment From: a631807682
refer to https://github.com/go-gorm/gorm/issues/6751#issuecomment-1871839537