第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 gorm 中創(chuàng)建一對一關系和外鍵?

如何在 gorm 中創(chuàng)建一對一關系和外鍵?

Go
犯罪嫌疑人X 2022-12-19 11:54:13
Has One和Has Many有什么區(qū)別 Belong To我有 3 個模型UserProfile哪里profile和user應該有one to one關系Categorycategory應該foreign key去哪里usertype User struct {gorm.ModelEmail *stringName string...}type Profile struct {gorm.ModelPhone stringAddress string...}type Category struct {gorm.ModelName string}
查看完整描述

1 回答

?
DIEA

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

}


查看完整回答
反對 回復 2022-12-19
  • 1 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號