我在Postgres数据库中有2个模型,我在Golang中使用GORM进行管理:
// ACO ...
type ACO struct {
gorm.Model
OwnerID string `gorm:"not null"`
Rights pq.StringArray `gorm:"type:varchar(64)[]"`
GroupID uint
StackID uint
QACID uint
MCCID uint
}
type Stack struct {
gorm.Model
Name string `gorm:"not null,unique_index:idx_name_owner_id"`
OwnerID string `gorm:"not null,unique_index:idx_name_owner_id"`
Stacks []Stack `gorm:"ForeignKey:StackID"`
ACO []ACO
QAC []QAC
MCC []MCC
}
我想执行以下查询:
stack := &Stack{}
err := db.Model(&Stack{}).Preload("Stack.ACO").Where(`
id = ? AND
stack.ACO.owner_id = ? AND
stack.ACO.stack_id = ? AND
stack.ACO.rights 'read' = ANY (rights) OR
stack.ACO.rights 'admin_full' = ANY (rights)`, stackID, issuerID, stackID).First(stack).Error
但是出现以下错误:Error: pq: missing FROM-clause entry for table "aco"
描述非常清楚,但是由于我对sql不太熟悉,因此我不确定如何通过GORM为aco表添加FROM子句。由于此查询似乎很基础,因此我很确定在建模模型时做错了什么。感谢您的任何帮助:D谢谢!