繁体   English   中英

One2Many 关系没有带有 ID 的基础 model

[英]One2Many Relation does not have base model with ID

我们目前有以下架构;


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 Relation("Reaction.*")替换Relation("Reaction.type") ) 我们没有收到错误(尽管CreatorPost都是 null),但是我们'正在检索比我们想要的更多的列。

我不是专业人士,但不是 model 后的反应。 它应该是Reactions Reaction...而不是Reactions Reactions... 因为 model 是反应,而不是反应。 我希望它能解决问题,我不是傻子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM