簡體   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