4

我正在研究在 PostgreSQL 中有一个执行数据库的 bun。这两个表之间存在关系。Order并且表ResPartnerOrder外键是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. 有没有办法覆盖字段名称?

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

4

1 回答 1

1

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

属于:

 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"`
}
于 2021-12-21T07:56:10.763 回答