One2Many关系没有具有ID的基本模型。

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

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.*"),我们就不会得到错误(尽管CreatorPost都为空),但是我们会检索到比我们想要的更多的列。

英文:

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.

huangapple
  • 本文由 发表于 2022年9月27日 23:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/73870024.html
匿名

发表评论

匿名网友

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

确定