-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
[Bug] Change assoc many
slice and Update
/Insert
with Changeset
#308
Comments
assoc many
slice and Update
with Changeset
assoc many
slice and Update
/Insert
with Changeset
Full example: type Parent struct {
ID int `db:",primary"`
Childs []*Child `ref:"id" fk:"parent_id" autosave:"true" autoload:"true"`
}
type Child struct {
ID int `db:",primary"`
ParentID int
Name string
}
func(repo rel.Repository) {
ctx := context.Background()
repo.Exec(ctx, "CREATE TABLE IF NOT EXISTS parents (id int PRIMARY KEY)")
repo.Exec(ctx, "CREATE TABLE IF NOT EXISTS children (id int PRIMARY KEY, parent_id int NOT NULL, name text, FOREIGN KEY (parent_id) REFERENCES parents (id))")
p := new(Parent)
if err := repo.Find(ctx, p, rel.Eq("id", 1)); err != nil {
p.ID = 1
p.Childs = []*Child{
{ID: 1, ParentID: 1, Name: "First"},
{ID: 2, ParentID: 1, Name: "Second"},
}
repo.Insert(ctx, p)
}
changeset := rel.NewChangeset(p)
// panic: rel: invalid mutator
if false {
p.Childs = append(p.Childs, &Child{ID: 3, ParentID: 1, Name: "Third"})
repo.Update(ctx, p, changeset)
}
// panic: rel: invalid mutator
if false {
p.Childs = []*Child{p.Childs[1]}
repo.Update(ctx, p, changeset)
}
// DELETE FROM "children" WHERE "children"."parent_id"=$1;
// INSERT INTO "children" ("name","parent_id") VALUES ($1,$2),($3,$4) RETURNING "id";
if false {
p.Childs[0].Name = "Foo"
p.Childs[1].Name = "Bar"
repo.Update(ctx, p, changeset)
}
} |
@qRoC created a related fix here #311, please test it if you can but, since your example is not using primary key generated in database, it's tricky to make repo.Update call successful |
Append - no panic, but generate queries: DELETE FROM "children" WHERE "children"."parent_id"=$1;
INSERT INTO "children" ("parent_id","id","name") VALUES ($1,DEFAULT,DEFAULT),($2,DEFAULT,DEFAULT),($3,$4,$5) RETURNING "id" Update - no panic, but generate queries: DELETE FROM "children" WHERE "children"."parent_id"=$1;
INSERT INTO "children" ("name","parent_id") VALUES ($1,$2),($3,$4) RETURNING "id"
Main problem - rel generates absolutely wrong queries:
|
I will do a PR with fix today |
Nested association of has many is not supported at the moment because of complexity in the implementation |
Can you show an example? I see only one case - ID is not serial + no changeset. |
This fix my issue: qRoC@45adbeb I don't create a PR because it changes the default behavior. |
yes, if not using changeset, the only way to tell a record is new or not is just by checking the primary field |
Append
Example
Actual result
Change
Example
Actual result
generate invalid queries
The text was updated successfully, but these errors were encountered: