![](/img/trans.png)
[英]Gorm belongs-to relation with same foreignKey and referenced primary key name
[英]bun:go: how to change `belongs-to` relation mapping field in bun
我正在研究在 PostgreSQL 中有一個執行數據庫的 bun。 兩個表之間存在關系。 Order
和ResPartner
,其中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.