D1 foreign keys: `PRAGMA defer_foreign_keys = off` clears outstanding violations instead of failing
Existing documentation URL(s)
- https://developers.cloudflare.com/d1/sql-api/foreign-keys/
- https://developers.cloudflare.com/d1/sql-api/sql-statements/ (same example and wording, under
PRAGMA defer_foreign_keys)
What changes are you suggesting?
The foreign keys page says (L42):
You can also explicitly set
PRAGMA defer_foreign_keys = offimmediately after you have resolved outstanding foreign key constraints. If there are still outstanding foreign key constraints, you will receive aFOREIGN KEY constraint failederror and will need to resolve the violation.
SQLite does the opposite (pragma docs):
Caution: Setting "defer_foreign_keys=0" resets the internal foreign-key tracking state of the database connection, so that it becomes possible to commit the current transaction even if there are foreign key violations.
In the source, turning the pragma off zeroes both deferred-violation counters (pragma.c L1177-1181), and the commit-time check only fails when they are non-zero (vdbeaux.c L3328-3332). So off with violations still outstanding doesn't error. The transaction commits and the orphaned rows stay.
The example on the page ends the same way (L38-39):
-- This is implicit if not set by the end of the transaction.
PRAGMA defer_foreign_keys = off
It isn't equivalent. At commit SQLite checks the deferred violations and fails, but an explicit off discards them first. L27 has the same gap: "If you have not resolved outstanding foreign key violations at the end of your transaction, it will fail" stops being true once off has run.
Repro with wrangler d1 migrations apply --local (wrangler 4.156.2):
-- migrations/0001_init.sql
CREATE TABLE p (id INTEGER PRIMARY KEY);
CREATE TABLE c (id INTEGER PRIMARY KEY, p_id INTEGER REFERENCES p (id));
INSERT INTO p VALUES (1), (2), (3);
INSERT INTO c VALUES (10, 1), (20, 2), (30, 3);
-- migrations/0002_drop_parent.sql
PRAGMA defer_foreign_keys = on;
DROP TABLE p;
PRAGMA defer_foreign_keys = off;
Both migrations apply. p is gone, and wrangler d1 execute <db> --local --command "PRAGMA foreign_key_check" returns 3 rows (c rowid 10, 20, 30 → p).
Without the last line, 0002 fails with FOREIGN KEY constraint failed: SQLITE_CONSTRAINT (extended: SQLITE_CONSTRAINT_FOREIGNKEY) and rolls back. p is still there, and only 0001 is recorded in d1_migrations.
Suggested fix, on both pages:
- Replace L42 with something like: "Setting
PRAGMA defer_foreign_keys = offdiscards any outstanding violations, and the transaction then commits with them. Leave it on until the end of the transaction so the commit check catches them, or runPRAGMA foreign_key_checkand resolve every row it returns before setting it tooff." - Drop the trailing
PRAGMA defer_foreign_keys = offfrom the example, or change its comment. - In L27 (and the matching sentence on sql-statements), add "unless
defer_foreign_keyswas set tooffwhile violations were outstanding".
Additional information
- The L42 wording comes from #21403 / #21704, which fixed
on→off. The sentence about the error stayed as it was. - Related: cloudflare/workers-sdk#5438 and cloudflare/workers-sdk#8512.
PRAGMA foreign_keys = OFFhas no effect inside a migration, so people are pointed todefer_foreign_keys, and #8512 links this page for it. - I only tested
--local. Does remote D1 fail on the migration above, as the page says, or commit like local? If they differ, the page should say which.