Skip to content
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

Persist structures with no data fields besides id #330

Open
grossvater opened this issue May 10, 2023 · 1 comment
Open

Persist structures with no data fields besides id #330

grossvater opened this issue May 10, 2023 · 1 comment

Comments

@grossvater
Copy link

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
Copy link
Member

Fs02 commented May 13, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants