Results.

SCEPTRA.TECH

SQLite Foreign Keys: Check the Writing Connection, Not Just the Table Definition

Original SQLite diagram separating declared relationships, connection enforcement and existing data audit

A SQLite table can contain a REFERENCES clause without your application's current connection enforcing it. That distinction matters when a script inserts a child row whose parent does not exist. Looking at the table definition alone does not answer whether the rule was active when that write happened.

I would separate three questions: what relationships are declared, what the current connection enforces, and whether existing rows violate the declarations. They need different checks.

Check the connection that actually writes

Run this through the same application connection you are investigating:

PRAGMA foreign_keys;

For a SQLite build with foreign-key support, the result is 1 when enforcement is enabled and 0 when it is disabled. Do not infer the value from a tutorial or the result in a separate database browser.

SQLite documents that enforcement is configured separately for each database connection. A shell session returning 1 does not prove that a background importer or another connection in a pool also has it enabled. Applications should explicitly configure the behavior they require rather than depend on the default.

If the query returns no row instead of a single 0 or 1, investigate the SQLite version and build. The official documentation identifies old versions or builds without the necessary foreign-key support as possible reasons. Do not treat missing output as confirmation that protection is enabled.

Inspect the declared relationship separately

For an example child table named orders, use:

PRAGMA foreign_key_list('orders');

Replace the example name with your actual child table. This lists declared foreign-key constraints for that table. It does not itself enable enforcement or prove that every current row has a valid parent.

If no relationship is declared, toggling enforcement cannot invent the intended business rule. Establish what the schema is meant to require before proposing a schema change. A relationship you expect in application code is not automatically a database constraint.

Check existing data without repairing it automatically

PRAGMA foreign_key_check;

SQLite returns a row for each foreign-key violation found. The output identifies the child table, the child rowid, the referenced parent table and the constraint identifier. For a child table created WITHOUT ROWID, that rowid field is NULL. The constraint identifier corresponds to the first column returned by foreign_key_list.

A narrower check can target the declarations in one child table:

PRAGMA foreign_key_check('orders');

These checks report problems; they do not decide whether an orphaned record should be deleted, linked to a different parent or retained while a missing parent is restored. That decision needs knowledge of the data. On a large database, plan the audit's timing rather than assuming that a read-only check has no operational cost.

Also, PRAGMA integrity_check is not a substitute here. SQLite explicitly says it does not find foreign-key errors. Use the check intended for the question you are asking.

Enable enforcement at the right point

If your application requires foreign-key enforcement, SQLite provides this connection setting:

PRAGMA foreign_keys = ON;
PRAGMA foreign_keys;

This changes connection behavior, unlike the diagnostic queries above. Apply it deliberately during connection setup and verify the returned value. SQLite says changing the setting inside a pending transaction or savepoint has no effect. Sending the statement without an error therefore is not enough evidence that it took effect.

Do not commit or roll back unrelated work merely to make the setting change. Fix the connection initialization path and test the application's expected behavior in an appropriate environment. Existing data still needs its own audit; an enabled setting is not a report that historical rows are all valid.

My stopping point is a recorded enforcement result from the real writing connection, an understood declaration and a reviewed violation report. That is more useful than repeatedly retrying a failed import or deleting rows until the error disappears.

Sources

Comments