亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何使用gorm制作外鍵

如何使用gorm制作外鍵

Go
互換的青春 2023-05-02 10:19:57
我有這兩個模型:用戶模型:type User struct {    DBBase    Email    string `gorm:"column:email" json:"email"`    Password string `gorm:"column:password" json:"-"`}func (User) TableName() string {    return "t_user"}用戶信息模型:type UserInfo struct {    User      User   `gorm:"foreignkey:u_id;association_foreignkey:id"`    UID       uint   `gorm:"column:u_id" json:"-"`    FirstName string `gorm:"column:first_name" json:"first_name"`    LastName  string `gorm:"column:last_name" json:"last_name"`    Phone     string `gorm:"column:phone" json:"phone"`    Address   string `gorm:"column:address" json:"address"`}func (UserInfo) TableName() string {    return "t_user_info"}我想讓 UID 與用戶表的 ID 相關。這是創建用戶的函數:func (dao *AuthDAO) Register(rs app.RequestScope, user *models.User, userInfo *models.UserInfo) (userErr error, userInfoErr error) {    createUser := rs.Db().Create(&user)    userInfo.UID = user.ID    createUserInfo := rs.Db().Create(&userInfo)    return createUser.Error, createUserInfo.Error}我確實嘗試了 gorm 在文檔中寫的內容,但沒有成功: http://doc.gorm.io/associations.html
查看完整描述

4 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

筆記!

從 gorm 2.0 開始,這不再是必需的

解決辦法是在遷移數據庫的時候加入這一行:

db.Model(&models.UserInfo{}).AddForeignKey("u_id",?"t_user(id)",?"RESTRICT",?"RESTRICT")



查看完整回答
反對 回復 2023-05-02
?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

我發現以下代碼無需執行任何自定義遷移代碼即可正確創建外鍵;只是平常的AutoMigrate。



type Account struct {

    ID uint `gorm:"primary_key"`

}


type Transaction struct {

    ID        uint `gorm:"primary_key"`

    AccountID uint

    Account   Account `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`

}


我正在使用“Gorm 2.0”,它是gorm.io/gorm v1.23.3.


查看完整回答
反對 回復 2023-05-02
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

閱讀https://gorm.io/docs/belongs_to.html中的屬于關系 此外,這里有一個很好的例子: https: //medium.com/@the.hasham.ali/how-to-use-uuid -key-type-with-gorm-cc00d4ec7100


// User is the model for the user table.

type User struct {

 Base

 SomeFlag bool    `gorm:"column:some_flag;not null;default:true"`

 Profile  Profile

}// Profile is the model for the profile table.

type Profile struct {

 Base

 Name   string    `gorm:"column:name;size:128;not null;"`

 UserID uuid.UUID `gorm:"type:uuid;column:user_foreign_key;not null;"`

}


查看完整回答
反對 回復 2023-05-02
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

我們可以在最新版本中使用添加外鍵約束CreateConstraint。


示例:假設我們有兩個實體


type User struct {

  gorm.Model

  CreditCards []CreditCard

}


type CreditCard struct {

  gorm.Model

  Number string

  UserID uint

}

現在為用戶和信用卡創建數據庫外鍵


db.Migrator().CreateConstraint(&User{}, "CreditCards")

db.Migrator().CreateConstraint(&User{}, "fk_users_credit_cards")

轉換為 Postgres 的以下 SQL 代碼:


ALTER TABLE `credit_cards` ADD CONSTRAINT `fk_users_credit_cards` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)


查看完整回答
反對 回復 2023-05-02
  • 4 回答
  • 0 關注
  • 406 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

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

公眾號

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