test that shows UpdateColumns
inserts associations that are deleted
#832
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I am updating some columns in a model that has it's child associations loaded.
In another transaction that is running on another goroutine, i am hard deleting one of the child associations of the parent.
When the transaction that deletes the child association commits first, the
UpdateColums
method reinserts the deleted child row into the table.I expect that
UpdateColums
only performs an update on those columns, and does not perform any operations outside of this model.logs
`2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:83
[4.375ms] [rows:1] SELECT count(*) FROM information_schema.tables WHERE table_schema = CURRENT_SCHEMA() AND table_name = 'parents' AND table_type = 'BASE TABLE'
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:83
[7.772ms] [rows:0] CREATE TABLE "parents" ("id" bigserial,"created_at" timestamptz,"updated_at" timestamptz,"deleted_at" timestamptz,"name" text,PRIMARY KEY ("id"))
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:83
[1.796ms] [rows:0] CREATE INDEX IF NOT EXISTS "idx_parents_deleted_at" ON "parents" ("deleted_at")
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:83
[0.811ms] [rows:1] SELECT count(*) FROM information_schema.tables WHERE table_schema = CURRENT_SCHEMA() AND table_name = 'children' AND table_type = 'BASE TABLE'
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:83
[2.309ms] [rows:0] CREATE TABLE "children" ("id" bigserial,"created_at" timestamptz,"updated_at" timestamptz,"deleted_at" timestamptz,"parent_id" bigint,PRIMARY KEY ("id","parent_id"),CONSTRAINT "fk_parents_children" FOREIGN KEY ("parent_id") REFERENCES "parents"("id"))
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:83
[1.490ms] [rows:0] CREATE INDEX IF NOT EXISTS "idx_children_deleted_at" ON "children" ("deleted_at")
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:96
[1.975ms] [rows:1] INSERT INTO "parents" ("created_at","updated_at","deleted_at","name") VALUES ('2025-09-29 10:43:57.816','2025-09-29 10:43:57.816',NULL,'parent1') RETURNING "id"
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:102
[1.587ms] [rows:1] INSERT INTO "children" ("created_at","updated_at","deleted_at","parent_id") VALUES ('2025-09-29 10:43:57.818','2025-09-29 10:43:57.818',NULL,1) RETURNING "id"
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:105
[1.145ms] [rows:1] INSERT INTO "children" ("created_at","updated_at","deleted_at","parent_id") VALUES ('2025-09-29 10:43:57.82','2025-09-29 10:43:57.82',NULL,1) RETURNING "id"
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:118
[0.691ms] [rows:2] SELECT * FROM "children" WHERE "children"."parent_id" = 1 AND "children"."deleted_at" IS NULL
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:118
[1.629ms] [rows:1] SELECT * FROM "parents" WHERE id = 1 AND "parents"."deleted_at" IS NULL AND "parents"."id" = 1 ORDER BY "parents"."id" LIMIT 1
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:139
[1.211ms] [rows:1] DELETE FROM "children" WHERE parent_id = 1 AND id = 1
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:126
[1.193ms] [rows:2] INSERT INTO "children" ("created_at","updated_at","deleted_at","parent_id","id") VALUES ('2025-09-29 10:43:57.818','2025-09-29 10:43:57.818',NULL,1,1),('2025-09-29 10:43:57.82','2025-09-29 10:43:57.82',NULL,1,2) ON CONFLICT ("id","parent_id") DO UPDATE SET "parent_id"="excluded"."parent_id" RETURNING "id"
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:126
[2.261ms] [rows:1] UPDATE "parents" SET "name"='parent1-updated' WHERE "parents"."deleted_at" IS NULL AND "id" = 1
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:154
[0.589ms] [rows:2] SELECT * FROM "children" WHERE parent_id = 1 AND "children"."deleted_at" IS NULL
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:166
[0.421ms] [rows:2] SELECT * FROM "children" WHERE "children"."parent_id" = 1 AND "children"."deleted_at" IS NULL
2025/09/29 10:43:57 /Users/wouterf/Developer/gorm-playground/main_test.go:166
[0.845ms] [rows:1] SELECT * FROM "parents" WHERE id = 1 AND "parents"."deleted_at" IS NULL AND "parents"."id" = 1 ORDER BY "parents"."id" LIMIT 1
--- PASS: Test_UpdateColums_insert_deleted_associations (5.64s)
PASS
`