1 回答

TA貢獻1802條經(jīng)驗 獲得超10個贊
文檔顯示了要轉(zhuǎn)到結(jié)構(gòu)引用的外鍵標記。即,在您的情況下,這些應(yīng)該在地址和國家/地區(qū),而不是地址ID和國家/地區(qū)代碼。像這樣:
type User struct {
Address Address `gorm:"foreignKey:AddressID;references:ID"`
AddressID int `gorm:"column:address_id"`
}
type Address struct {
CountryCode int `gorm:"column:country_code"`
Country Country gorm:"foreignKey:CountryCode; references:Code"`
}
請嘗試使用這些。
為
請在此處查看急切加載
db.Preload("User").Preload("Address").Find(&users)
您可以在列上使用級聯(lián)標記。
type User struct {
gorm.Model
Name string
CompanyID int
Company Company `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}
- 1 回答
- 0 關(guān)注
- 107 瀏覽
添加回答
舉報