登录
首页 >  Golang >  Go问答

Bun:go:如何修改 Bun 中的关联关系字段

来源:stackoverflow

时间:2024-02-19 13:18:27 421浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《Bun:go:如何修改 Bun 中的关联关系字段》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

我正在开发一个在 postgresql 中有一个执行数据库的包。两个表之间存在关系。 orderrespartner,其中 order 表具有 respartner 表的外键,该表具有列名称 contact_partner

type order struct {
    bun.basemodel    `bun:"select:sb_order"`
    id               int64       `bun:"id"`
    genoid           string      `bun:"gen_oid"`
    contactpartner   *respartner `bun:"rel:belongs-to"`
    contactpartnerid int64       `bun:"contact_partner"`
}
type respartner struct {
    bun.basemodel `bun:"select:sb_partner"`
    id            int64  `bun:"id"`
    name          string `bun:"name"`
}

我尝试进行这样的查询。

err = db.NewSelect().Model(&order).
        Relation("ContactPartner").
        Scan(ctx)

但它给出了错误。

reflect:在ptr value上调用reflect.value.field

我认为bun尝试找到像contact_partner_id这样的字段名称。有什么办法可以覆盖字段名称吗?

更新:我已经更新了我的问题。例如,请参阅此存储库:go-db-test


正确答案


您可以在发髻中映射关系:类似于:

属于:

type user struct {
      id        int64 `bun:",pk"`
      name      string
      profileid int64
      profile   *profile `bun:"rel:belongs-to,join:profile_id=id"`
    }

有一个:

type User struct {
    ID      int64 `bun:",pk"`
    Name    string
    Profile *Profile `bun:"rel:has-one,join:id=user_id"`
}

到这里,我们也就讲完了《Bun:go:如何修改 Bun 中的关联关系字段》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>