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

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

如何使用 gorm.Preload 修復模式錯誤的不支持關系

如何使用 gorm.Preload 修復模式錯誤的不支持關系

Go
森欄 2023-01-03 17:03:21
我不斷收到Technician: unsupported relations for schema Ticket此結構模式和查詢的錯誤消息?我該怎么做才能使這個 Preload 查詢有效?或者至少如何調試這個問題?錯誤非常少,我已經閱讀了 gorm 預加載頁面https://gorm.io/docs/preload.html,但不明白我做錯了什么?type Ticket struct {    ID                  uuid.UUID  `json:"id"`    CreatedAt           time.Time  `json:"createdAt"`    UpdatedAt           time.Time  `json:"updatedAt"`    ShopID              uuid.UUID  `json:"shopID"`    Archived            bool       `json:"archived"`    Services            []string   `json:"services"`    Price               int        `json:"price"`    Location            int        `json:"location"`    Checkedout          bool       `json:"checkedout"`    TechnicianID        uuid.UUID  `json:"technicianId"`    Technician          Technician `json:"technician"`    TechnicianPartnerID *uuid.UUID `json:"technicianPartnerId"`    LastUpdatedBy       uuid.UUID  `json:"lastupdatedBy"`}type Technician struct {    ID        uuid.UUID `json:"id"`    CreatedAt time.Time `json:"createdAt"`    UpdatedAt time.Time `json:"updatedAt"`    ShopID    uuid.UUID `json:"shopID"`    Name      string    `json:"name"`    Active    bool      `json:"active"`}dbQuery := t.Db.Orm.WithContext(ctx).Table("tickets").Preload("Technician")
查看完整描述

1 回答

?
心有法竹

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

您沒有使用標準的 gorm.Model 作為鍵(來自文檔):


type Model struct {

  ID        uint           `gorm:"primaryKey"`

  CreatedAt time.Time

  UpdatedAt time.Time

  DeletedAt gorm.DeletedAt `gorm:"index"`

}

Gorm 使用它來識別連接。


gorm:"primaryKey"使用指示器更改鍵應該可以解決問題。


或者替代方案:使用 gorm.Model:


type Ticker struct {

    gorm.Model

    ShopID              uuid.UUID  `json:"shopID"`

    Archived            bool       `json:"archived"`

    Services            []string   `json:"services"`

    Price               int        `json:"price"`

    Location            int        `json:"location"`

    Checkedout          bool       `json:"checkedout"`

    TechnicianID        uuid.UUID  `json:"technicianId"`

    Technician          Technician `json:"technician"`

    TechnicianPartnerID *uuid.UUID `json:"technicianPartnerId"`

    LastUpdatedBy       uuid.UUID  `json:"lastupdatedBy"`

}


type Technician struct {

    gorm.Model

    ShopID    uuid.UUID `json:"shopID"`

    Name      string    `json:"name"`

    Active    bool      `json:"active"`

}


查看完整回答
反對 回復 2023-01-03
  • 1 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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