go-rel / rel

:gem: Modern ORM for Golang - Testable, Extendable and Crafted Into a Clean and Elegant API
https://go-rel.github.io/
MIT License
744 stars 58 forks source link

Persist structures with no data fields besides id #330

Open grossvater opened 1 year ago

grossvater commented 1 year ago

I have a mapping like this:

type Product struct {
    Id           int
    PropertyGroup   PropertyGroup `ref:"property_group_id" fk:"id" autosave:"true" autoload:"true"`
    PropertyGroupId int
}

type PropertyGroup struct {
    Id         int
    Properties []Property `ref:"id" fk:"group_id" autosave:"true" autoload:"true"`
}

type Property struct {
    Id    int
    Key   string
    Value string

    Group   PropertyGroup `ref:"group_id" fk:"id"`
    GroupId int
}

Please note that what's important here is the PropertyGroup, which has no data fields. I kept Property in sample only to show what I want to achieve and why I might need such an empty table.

When doing the insert for Product, with a reference to an existing PropertyGroup, I have this error: pq: syntax error at or near "WHERE"

Generated sql is this: UPDATE "property_groups" SET WHERE "property_groups"."id"=$1; - pq: syntax error at or near "WHERE"

Of course an workaround might be to remove the autosave:"true" between Product and PropertyGroup, but this would break the insert when the PropertyGroup doesn't exist.

Fs02 commented 1 year ago

Of course an workaround might be to remove the autosave:"true" between Product and PropertyGroup, but this would break the insert when the PropertyGroup doesn't exist.

one possible fix that I can think of is to not do any updates/insert if the struct only have primary fields, but this will still break the insert if property group doesn't exists 🤔

with a reference to an existing PropertyGroup

seems the best way to go here is to disable autosave and set property group using PropertyGroupId