英文:
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"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论