r/sqlite • • 4d ago

SQLite foreign keys are off by default, per connection: four small experiments (tested on 3.53.1)

If you declare REFERENCES in your schema and assume the database enforces it, here’s something worth checking. I ran four small experiments on SQLite 3.53.1 from Python.

Schema:

CREATE TABLE parent (id INTEGER PRIMARY KEY);
CREATE TABLE child (
    id INTEGER PRIMARY KEY,
    parent_id INTEGER REFERENCES parent(id) ON DELETE CASCADE
);
INSERT INTO parent VALUES (1);

1. Default connection

PRAGMA foreign_keys;                       -- 0
INSERT INTO child (parent_id) VALUES (999);  -- accepted, no error

The orphan row was accepted. With foreign_keys at 0, the REFERENCES clause isn’t enforced.

2. After turning it on

PRAGMA foreign_keys = ON;
PRAGMA foreign_keys;                       -- 1
INSERT INTO child (parent_id) VALUES (999);
-- FOREIGN KEY constraint failed
INSERT INTO child (parent_id) VALUES (1);
DELETE FROM parent WHERE id = 1;           -- cascades; child is now empty

Enforcement works, including ON DELETE CASCADE (0 child rows left).

3. Inside a transaction, the pragma did nothing

BEGIN;
PRAGMA foreign_keys = ON;
PRAGMA foreign_keys;   -- 0 (still off)
COMMIT;
PRAGMA foreign_keys;   -- 0

In my test, turning it on inside an open transaction had no effect, and it stayed off after the commit. As I understand the docs, the setting can’t be changed while a transaction is open, so set it right after opening the connection, before any BEGIN. Check the official documentation for the exact wording.

4. It’s per connection, not per database file

connection a (set ON):  1
connection b (same file, new connection): 0

The setting isn’t stored in the database file, so each new connection starts with the default again. A second process or a different tool opening the same file may not enforce your foreign keys.

What I’d do about it (opinion):

  • Run PRAGMA foreign_keys = ON right after every connection is opened, ideally in the one place your code creates connections (a connection factory or pool hook).
  • Add a test that tries to insert an orphan row and expects it to fail, so a future change that bypasses the setup gets caught.
  • To audit existing data, PRAGMA foreign_key_check lists violations. I haven’t run it in these experiments, so check how it behaves on your database.
  • Some libraries and drivers enable it for you and others don’t, so check yours.

Limits: this is one SQLite version and one driver (Python’s sqlite3), and the behavior can be changed by compile-time options, which I haven’t tested. If you use an ORM, it may set the pragma for you.

Do you enable foreign keys in a connection hook, a test, or do you leave them off? Any surprises from tools opening the same file?

1 Upvotes

2 comments sorted by

3

u/trailbaseio 4d ago

Experiments are fun but better check out section 2 of https://www.sqlite.org/foreignkeys.html (nothing is python specific and the docs address the build options too)

1

u/neprotivo 3d ago

I used exactly the same problem to demonstrate what I'm working on - a way to do formal verification of SQL statements. Here's the writeup: https://vihren.dev/blog/20261112-belay/ If you forget that foreign keys PRAGMA has no effect in a transaction, you can delete your data! This has happened already to people.