What this thread does
Do first: set PRAGMA foreign_keys=ON outside a transaction on every connection, then require PRAGMA foreign_keys to return 1 before accepting writes.
Short answer
As of November 2026, the release baseline is SQLite 3.54.1, October 16, 2026. Make the business operation own the explicit rollback path.
Choose / don't choose
- Immediate constraints: choose for early rejection; an immediate foreign key violation rolls back the current statement transaction (foreign keys).
- Deferred constraints: choose when related rows must be written out of dependency order; don't return success before COMMIT succeeds.
- Savepoint: choose for an independently discardable sub-operation; don't keep earlier business-operation writes if the entire operation must fail (savepoints).
Not for: database selection; see sqlite-or-postgres-for-a-small-production-app-litestream-d1-turso.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
PRAGMA foreign_keys | Set ON, read back 1; per connection; enabling or disabling during a multi-statement transaction has no effect | Current SQLite docs, November 2026 | foreign keys |
PRAGMA defer_foreign_keys | ON for the current transaction; default OFF, reset by COMMIT/ROLLBACK | Current SQLite docs, November 2026 | pragma |
DEFERRABLE INITIALLY DEFERRED | Declares a deferred foreign key constraint | Current SQLite docs, November 2026 | deferred constraints |
foreign_key_check pragma | Returns one row per violation; inspect violations before declaring integrity restored | Current SQLite docs, November 2026 | pragma |
ROLLBACK TO | Revert the database to just after the corresponding SAVEPOINT; transaction remains active | Current SQLite docs, November 2026 | savepoints |
Do not toggle deferral OFF to force a commit: the pragma reference warns that it resets tracking. Keep enforcement enabled for the connection. A foreign-key pragma returning no row indicates unsupported enforcement; fail connection initialization.
Order of operations
1. In a disposable database, create parent and child fixtures with enforcement enabled. Capture row counts and business values before the operation. 2. Begin one transaction, perform an earlier valid write, then insert a child without its required parent. On the immediate constraint exception explicitly ROLLBACK; inspect from a fresh transaction and require all operation changes absent. 3. Repeat using the defer_foreign_keys pragma to temporarily defer all constraints: perform the invalid related writes and attempt COMMIT. Require commit failure, then explicitly ROLLBACK on that same connection; a deferred violation leaves the transaction open. Confirm a fresh BEGIN succeeds and no operation changes remain (foreign keys). 4. For an optional sub-operation, create SAVEPOINT before its first write, use ROLLBACK TO operation on failure, then RELEASE operation. Continue the outer transaction only if its business contract permits retaining the earlier writes; inner RELEASE is not durable commit (savepoints). 5. Run the successful path and PRAGMA foreign_key_check; require no violations and the complete expected business result. Check pool checkout initialization too, not just the first connection.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
SQLITE_CONSTRAINT_FOREIGNKEY | Immediate FK violation aborts the statement | Roll back the entire owned operation before retry/return | code, implementation |
foreign key constraint failed | Deferred foreign key constraints are checked when the transaction tries to COMMIT | Keep connection reserved; repair deliberately or ROLLBACK, then return it to the pool | foreign keys |
cannot start a transaction within a transaction | Failed COMMIT left an active transaction on a reusable connection | Ensure commit-error cleanup issues ROLLBACK; discard a connection if cleanup itself fails | lehni, October 15, 2026 |
Lehni's Knex report documents the reusable-connection failure; it is an adapter report, not a SQLite promise that adapters automatically roll back.
Missing
- Operation-specific fixtures and adapter transaction APIs are unspecified. RESTRICT actions and cascade effects need separate assertions when present.
Started from a post by @SQLite, packaged by the editor. Original post