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

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

插入后無法檢索任何關系表

插入后無法檢索任何關系表

Go
30秒到達戰場 2022-09-26 14:48:59
我正在使用 與 ,我有如下所示的表格。我在檢索關系表時遇到問題,例如,當我插入新用戶時,所有內容都按下面的日志所示插入,但是一旦我嘗試檢索該用戶,那么所有應該是帶有外表的字段的字段要么是或。從我在數據庫中看到的內容來看,這些表的值入并且實際上已經創建了外鍵,那么問題可能是什么呢?gormGolangnil[]模型:type User struct {    ID          string `json:"id" gorm:"primaryKey"`    CreatedAt   time.Time    UpdatedAt   time.Time    DeletedAt   gorm.DeletedAt `gorm:"index"`    License     *License       `json:"license"`    Statistics  []*Statistic   `json:"statistics"`    App         *App           `json:"app"`    Disabled    bool           `json:"disabled"`    EncryptedID string         `json:"encrypted_id"`}//1 of the tables that doesn't get found but exists in database after creationtype License struct {    gorm.Model    Key               string  `json:"key"`    DesktopHardwareID string  `json:"desktop_hardware_id"`    MobileHardwareID  *string `json:"mobile_hardware_id"`    Stripe            *Stripe  `json:"stripe" ,gorm:"foreignKey:LicenseID"`    UserID            string  `json:"user_id"`}提前致謝編輯 1 檢索行的代碼var user models.User//tokens[0] is the ID of the userdb.PostgresClient.Where("id = ?", tokens[0]).First(&user)
查看完整描述

1 回答

?
開心每一天1111

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

要從關系表中加載數據,可以使用該函數。Preload

要加載所有第一級關聯,您可以執行如下操作:

db.PostgresClient.Preload(clause.Associations).Where("id = ?", tokens[0]).First(&user)

但是,對于嵌套關聯,您需要單獨執行它們:

db.PostgresClient.Preload("License.Stripe").Where("id = ?", tokens[0]).First(&user)

您可以查看文檔以獲取更多詳細信息。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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