为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 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 关注
  • 160 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号