bun icon indicating copy to clipboard operation
bun copied to clipboard

Override default polymorphic column value

Open comsma opened this issue 2 years ago • 1 comments

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"`
}

comsma avatar Jan 16 '23 21:01 comsma

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?

smyrman avatar Feb 06 '23 13:02 smyrman