繁体   English   中英

bun:go:如何更改 bun 中的`belongs-to` 关系映射字段

[英]bun:go: how to change `belongs-to` relation mapping field in bun

我正在研究在 PostgreSQL 中有一个执行数据库的 bun。 两个表之间存在关系。 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: call of reflect.Value.Field on ptr Value

我认为 bun 尝试找到一个字段名称,例如contact_partner_id 有没有办法覆盖字段名称?

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

您可以在 bun:go 中映射关系,例如:

属于:

 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"`
}

暂无
暂无

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

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