1 回答
TA贡献1820条经验 获得超2个赞
为了User Has One Profile
type User struct {
gorm.Model
Email *string
Name string
Profile Profile //this is the key different
}
type Profile struct {
gorm.Model
UserId int //this is important
Phone string
Address string
}
为了Profile Belong To User
type User struct {
gorm.Model
Email *string
Name string
}
type Profile struct {
gorm.Model
UserId int //this is important
User User //this is the key different
Phone string
Address string
}
为了User Has Many Category
type User struct {
gorm.Model
Email *string
Name string
CategoryList []Category
}
type Category struct {
gorm.Model
UserId int //this is important
Name string
}
编辑:UserId 字段将成为您的外键。
如果你想让 gorm 自动为你创建表,你可以AutoMigrate在 main.go中使用
err := db.AutoMigrate(your_model_package.User{})
if err != nil {
return err
}
- 1 回答
- 0 关注
- 164 浏览
添加回答
举报