【发布时间】:2021-07-06 17:31:39
【问题描述】:
我想在 GORM V2 中订购带有关联记录的记录。
我有两个结构:
type Client struct {
gorm.Model
UUID uuid.UUID `gorm:"type:uuid"`
ClientProfile ClientProfile
}
type ClientProfile struct {
gorm.Model
ClientID uint
FirstName string
LastName string
}
现在,我可以加载所有记录:
db.
Preload(clause.Associations).
Find(&clients).
我现在如何使用 ClientProfile 结构中的字段对这些记录进行排序?
我试过没有成功:
db.
Preload(clause.Associations).
Order("client_profiles.last_name DESC").
Find(&clients)
这会引发以下错误:
错误:缺少表“client_profiles”的 FROM 子句条目 (SQLSTATE 42P01)
【问题讨论】: