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

huangapple go评论151阅读模式
英文:

How to fix unsupported relations for schema error with gorm.Preload

问题

我一直在收到错误消息“Technician: unsupported relations for schema Ticket”,针对这个结构模式和查询?我该怎么做才能使这个预加载查询工作?

或者至少我该如何调试这个问题?错误信息非常简洁,我已经阅读了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")
英文:

I keep getting error Technician: unsupported relations for schema Ticket for this struct schema and query? What can I do to get make this Preload query works?

Or at least how can debug this issue? The error is pretty bare minimal and I have read the gorm preload page https://gorm.io/docs/preload.html, and don't get what I did wrong?

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

得分: 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"`
}
英文:

You are not using the standard gorm.Model for the keys (from the docs):

type Model struct {
  ID        uint           `gorm:"primaryKey"`
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt gorm.DeletedAt `gorm:"index"`
}

Gorm uses this to identify joins.

Changing your keys with the gorm:"primaryKey" indicator should fix the issue.

Or alternative: use 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"`
}

huangapple
  • 本文由 发表于 2022年7月23日 08:38:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/73087262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定