-
-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PostgreSQL Composite Types support #415
Comments
Bun does not support composite types. If you are interested, you could try to port that feature from go-pg. |
This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed. |
We have some good cases where we would like to use composite types. Which option would be preferred? Declare type in composite modelRequire new struct tag usage. type inventoryItem struct {
bun.BaseModel `bun"type:myschema.inventory_item"`
Name string `bun:"name"`
SupplierID int `bun:"supplier_id"`
Price float64 `bun:"price"`
}
type onHand struct {
bun.BaseModel `bun:"table:myschema.on_hand"`
InventoryItem inventoryItem `bun:"inventory_item"`
Count int `bun:"count"`
} Declare type when usedDoesn't require new struct tag usage. type inventoryItem struct {
Name string `bun:"name"`
SupplierID int `bun:"supplier_id"`
Price float64 `bun:"price"`
}
type onHand struct {
bun.BaseModel `bun:"table:myschema.on_hand"`
InventoryItem inventoryItem `bun:"inventory_item,type:myschema.intentory_item"`
Count int `bun:"count"`
} EDIT: Forgot to add |
In your first example, In your 2nd example, it is not obvious that It probably should be more explicit: type onHand struct {
bun.BaseModel `table:"myschema.on_hand"`
InventoryItem inventoryItem `bun:"inventory_item,type:myschema.intentory_item,composite"`
Count int `bun:"count"`
} |
I forgot the bun prefix on some of the stuct tags in my previous comment; fixed now.
Don't I need to do
Does it have to be obvious? I see Gorm (according to Atlas at least), just use a I think that as a user, I wouldn't really need to care if the referenced type is a simple type or a composite type, would I? |
Right, I see |
Trying to understand the code for where this would have to be added. I found there is already matching done on the key
So to understand what's lacking here, we probably need a new To get started quicker, is there an easy way for me to do this with an type inventoryItem struct {
bun.BaseModel `bun"type:myschema.inventory_item"`
Name string `bun:"name"`
SupplierID int `bun:"supplier_id"`
Price float64 `bun:"price"`
}
func (inventoryItem) Value() (driver.Value, error) {
var b []byte
b = append(b, '(')
// What goes here?
b = append(b, ')')
return b, nil
}
func (inventoryItem) Scan(src any) error {
b, ok := src.([]byte)
if !ok {
return fmt.Errorf("unexpected type; got %T, want []byte", src)
}
b = bytes.TrimSpace(b)
if b[0] != '(' || b[len(b)-1] != ')' {
return fmt.Errorf("expecting surrounding parenthesis")
}
b = b[1 : len(b)-1]
// What goes here?
} And is this likely to work for array fields? type onHand struct {
bun.BaseModel `table:"myschema.on_hand"`
InventoryItems []inventoryItem `bun:"inventory_items,composite:myschema.intentory_item,array"`
} |
How to make model for Composite Types?
Ref: https://www.postgresql.org/docs/9.6/rowtypes.html
The text was updated successfully, but these errors were encountered: