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 自動為你創(chuàng)建表,你可以AutoMigrate在 main.go中使用
err := db.AutoMigrate(your_model_package.User{})
if err != nil {
return err
}
- 1 回答
- 0 關注
- 189 瀏覽
添加回答
舉報