| | 9 | Deferred sql correctly runs before record_migration, as in the fix for #32374, but now also in the transaction. |
| | 10 | |
| | 11 | The issue is clear from the [abbreviated] postgres logs. The django_migrations insert happens after the COMMIT of the main migration, so the migration isn't really atomic. |
| | 12 | |
| | 13 | Before: |
| | 14 | {{{ |
| | 15 | BEGIN |
| | 16 | CREATE TABLE "foo_foo" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "a_id" bigint NOT NULL) |
| | 17 | ALTER TABLE "foo_foo" ADD CONSTRAINT "foo_foo_a_id_f8ce5994_fk_foo_foo_id" FOREIGN KEY ("a_id") REFERENCES "foo_foo" ("id") DEFERRABLE INITIALLY DEFERRED |
| | 18 | CREATE INDEX "foo_foo_a_id_f8ce5994" ON "foo_foo" ("a_id") |
| | 19 | COMMIT |
| | 20 | INSERT INTO "django_migrations" ("app", "name", "applied") VALUES ('foo', '0001_initial', '2026-10-07 07:10:08.085619+00:00'::timestamptz) RETURNING "django_migrations"."id" |
| | 21 | }}} |
| | 22 | |
| | 23 | After patch: |
| | 24 | {{{ |
| | 25 | BEGIN |
| | 26 | CREATE TABLE "foo_foo" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "a_id" bigint NOT NULL) |
| | 27 | ALTER TABLE "foo_foo" ADD CONSTRAINT "foo_foo_a_id_f8ce5994_fk_foo_foo_id" FOREIGN KEY ("a_id") REFERENCES "foo_foo" ("id") DEFERRABLE INITIALLY DEFERRED |
| | 28 | CREATE INDEX "foo_foo_a_id_f8ce5994" ON "foo_foo" ("a_id") |
| | 29 | INSERT INTO "django_migrations" ("app", "name", "applied") VALUES ('foo', '0001_initial', '2026-10-07 07:13:01.602656+00:00'::timestamptz) RETURNING "django_migrations"."id" |
| | 30 | COMMIT |
| | 31 | }}} |
| | 32 | |
| | 33 | The django_migrations insert happens both after the deferred sql and inside the transaction, so it is atomic. |