Skip to content

Conversation

Wouter12
Copy link

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
`

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

Successfully merging this pull request may close these issues.

2 participants