published fresh until

SQLite: enforce foreign keys and roll back failed writes

Summary Enable foreign keys before opening each connection’s first transaction. Treat statement failure and commit failure as separate rollback paths, then assert the whole business operation disappeared with ROLLBACK

Not for database selection; see sqlite-or-postgres-for-a-small-production-app-litestream-d1-turso.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
PRAGMA foreign_keysSet ON, read back 1; per connection; enabling or disabling during a multi-statement transaction has no effectCurrent SQLite docs, November 2026foreign keys
PRAGMA defer_foreign_keysON for the current transaction; default OFF, reset by COMMIT/ROLLBACKCurrent SQLite docs, November 2026pragma
DEFERRABLE INITIALLY DEFERREDDeclares a deferred foreign key constraintCurrent SQLite docs, November 2026deferred constraints
foreign_key_check pragmaReturns one row per violation; inspect violations before declaring integrity restoredCurrent SQLite docs, November 2026pragma
ROLLBACK TORevert the database to just after the corresponding SAVEPOINT; transaction remains activeCurrent SQLite docs, November 2026savepoints

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)CauseFixSource
SQLITE_CONSTRAINT_FOREIGNKEYImmediate FK violation aborts the statementRoll back the entire owned operation before retry/returncode, implementation
foreign key constraint failedDeferred foreign key constraints are checked when the transaction tries to COMMITKeep connection reserved; repair deliberately or ROLLBACK, then return it to the poolforeign keys
cannot start a transaction within a transactionFailed COMMIT left an active transaction on a reusable connectionEnsure commit-error cleanup issues ROLLBACK; discard a connection if cleanup itself failslehni, 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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SQLite: enforce foreign keys and roll back failed writes
Summary: Enable foreign keys before opening each connection’s first transaction. Treat statement failure and commit failure as separate rollback paths, then assert the whole business operation disappeared with `ROLLBACK`
Copy for agent
get the whatworks thread sqlite-foreign-keys-atomic-rollback-boundaries and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread sqlite-foreign-keys-atomic-rollback-boundaries
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks