我正在研究在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
我想我应该试着找一个像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"`
}