我正在试图弄清楚如何加载与 gorm
的关联 . 文档不是很好 .
我要模特儿:
type Account struct {
gorm.Model
Name string `gorm:"column:name;unique_index;not null;size:255"`
AccountTypeID uint `gorm:"not null"`
UserID uint `gorm:"not null"`
AccountType accounttypes.AccountType
}
和
type AccountType struct {
gorm.Model
Name string `gorm:"column:name;unique_index;not null;size:255"`
Type string `gorm:"column:type;unique_index;not null;size:255"`
}
所以每个 Account
都有 Account Type
与之关联 .
当我尝试通过以下方式加载数据时:
func GetAccounts(userId uint) ([]Account, error) {
db := common.GetDB()
var model []Account
db.LogMode(true)
err := db.Model(&Account{}).Where("accounts.user_id =?", userId).Related(&accounttypes.AccountType{}, "AccountTypes").Error
return model, err
}
这是输出:
有人可以解释如何正确地做到这一点'gorm方式'?