uptrace / bun

SQL-first Golang ORM
https://bun.uptrace.dev
BSD 2-Clause "Simplified" License
3.59k stars 218 forks source link

Override default polymorphic column value #757

Open comsma opened 1 year ago

comsma commented 1 year ago

I am trying to make my bun model based of an existing database schema. There is an existing table that has a column called TransType that contains the polymorphic key. When InventoryReceiptLine is using InvTran it is linked by an IRA key. The problem is when using buns polymorphic relations it expects the key to be InventoryReceiptsLine which it is not. Can I override what the string is stored in the polymorphic column?

InventoryReceiptLine

type InventoryReceiptLine struct {
    bun.BaseModel `bun:"table:inventory_receipts_line"`

    ReceiptNumber int32   `bun:"receipt_number,pk"`
    InvMastUid    int32   `bun:"inv_mast_uid"`

    OrderTransactions []*InvTran       `bun:"rel:has-many,join:inv_mast_uid=inv_mast_uid,join:type=trans_type,polymorphic"`
}

InvTran

type InvTran struct {
    bun.BaseModel `bun:"table:inv_tran"`

    TransactionNumber float32 `bun:"transaction_number,pk"`
    InvMastUid        int32   `bun:"inv_mast_uid"`
    TransType         string  `bun:"trans_type"`
}
smyrman commented 1 year ago

I would also very much like to be able to override the polymorphic type name.

Maybe instead of type, you could allow static values in JOIN, as in join:'my-type'=trans_type?