我是 Golang 和 Gorm 的新手,我找不到答案。在休息 api 中,我希望我的 json 带有来自一个表关系的值。
type Product struct {
gorm.Model
Name string
Description string
Weight string
TypeID uint
}
type Type struct {
ID string `gorm:"primaryKey;"`
Name string
Product []Product
}
我希望我的产品 json 带有来自类型的 ID 和名称。但这不起作用。
var product Product
id:=1
db.Preload("Type").First(&product, id)
我必须在结构中做这样的事情吗?
type Product struct {
gorm.Model
Name string
Description string
Weight string
Type Type
}
精慕HU