英文:
One2Many Relation does not have base model with ID
问题
我们目前有以下的模式:
type Reactions []*Reaction
type Post struct {
    ID string `pg:",pk" json:",omitempty"`
	CreatorID string `pg:",notnull"`
	Creator   *User  `pg:",rel:has-one,fk:creator_id"`
	Groups   Groups  
	Reactions Reactions `pg:",rel:has-many" json:",omitempty"`
}
type Reaction struct {
	ID string `pg:",pk" json:",omitempty"`
	Type ReactionType `pg:",notnull"`
	CreatorID string `pg:",notnull"`
	Creator   *User  `pg:",rel:has-one,fk:creator_id"`
	PostID string  `pg:",notnull"`
	Post   *Post `pg:",rel:has-one,fk:post_id"`
}
当尝试使用以下查询查询所有帖子及其反应关系时,我们收到以下错误消息:pg: relation="Reactions" does not have base model=Post with id="" (check join conditions)
func (pm PGPostRepo) selectQuery(model ...interface{}) *orm.Query {
	return pm.db.Model(model...).
		Relation("Creator.id").
		Relation("Creator.given_name").
		Relation("Creator.family_name").
		Relation("Reactions.type").
		Column("post.*").
		Where("post.id = ?", postID).
		Select()
}
对于这个问题,我实际上有点迷茫,因为如果我们将Relation("Reaction.type")替换为Relation("Reaction.*"),我们就不会得到错误(尽管Creator和Post都为空),但是我们会检索到比我们想要的更多的列。
英文:
We currently have the following schema;
type Reactions []*Reaction
type Post struct {
    ID string `pg:",pk" json:",omitempty"`
	CreatorID string `pg:",notnull"`
	Creator   *User  `pg:",rel:has-one,fk:creator_id"`
	Groups   Groups  
	Reactions Reactions `pg:",rel:has-many" json:",omitempty"`
}
type Reaction struct {
	ID string `pg:",pk" json:",omitempty"`
	Type ReactionType `pg:",notnull"`
	CreatorID string `pg:",notnull"`
	Creator   *User  `pg:",rel:has-one,fk:creator_id"`
	PostID string  `pg:",notnull"`
	Post   *Post `pg:",rel:has-one,fk:post_id"`
}
When trying to query all Posts including their Reaction Relations using the following query, we recieve the following error message; pg: relation=\"Reactions\" does not have base model=Post with id=\"\" (check join conditions)
func (pm PGPostRepo) selectQuery(model ...interface{}) *orm.Query {
	return pm.db.Model(model...).
		Relation("Creator.id").
		Relation("Creator.given_name").
		Relation("Creator.family_name").
		Relation("Reactions.type").
		Column("post.*")
		Where("post.id = ?", postID).
		Select()
}
I'm actually quite lost on this one, as if we replace
Relation("Reaction.type") with Relation("Reaction.*") we do not get the error(Though both Creator & Post are null), but then we're retrieving more columns than we'd like.
答案1
得分: 2
我不是专家,但是这不是“Reactions in Post”模型吗?应该是Reactions Reaction ...而不是Reactions Reactions ...。因为模型是Reaction,而不是Reactions。希望这解决了问题,我并不愚蠢。
英文:
I am not a pro but isn't it the Reactions in Post model. It should be Reactions Reaction ... instead of Reactions Reactions .... Because the model is Reaction, not Reactions. I hope it solves the problem and i am not a stupid.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论