猿问

如何使用 gorm.Preload 修复模式错误的不支持关系

我不断收到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")


森栏
浏览 163回答 1
1回答

心有法竹

您没有使用标准的 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"`}
随时随地看视频慕课网APP

相关分类

Go
我要回答