[英]bun:go: how to change `belongs-to` relation mapping field in bun
I am working on bun that has an execting database in PostgreSQL.我正在研究在 PostgreSQL 中有一个执行数据库的 bun。 There was a relationship between the two tables.
两个表之间存在关系。
Order
and ResPartner
where Order
table has a foreign key of ResPartner
table having column name contact_partner
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"`
}
I try to make queries like this.我尝试进行这样的查询。
err = db.NewSelect().Model(&order).
Relation("ContactPartner").
Scan(ctx)
But it is giving error.但它给出了错误。
reflect: call of reflect.Value.Field on ptr Value
I think bun try to find a field name like contact_partner_id
.我认为 bun 尝试找到一个字段名称,例如
contact_partner_id
。 Is there any way to override the field name?有没有办法覆盖字段名称?
UPDATE: I have updated my question.更新:我已经更新了我的问题。 see this repo for example: go-db-test
例如,请参阅此存储库: go-db-test
You can map relationships in a bun:go something like:您可以在 bun:go 中映射关系,例如:
belongs-to:属于:
type User struct {
ID int64 `bun:",pk"`
Name string
ProfileID int64
Profile *Profile `bun:"rel:belongs-to,join:profile_id=id"`
}
has-one:有一个:
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.