Skip to content
Anonyfox

SQLite On Delete Callbacks

Sigil board: this spell's code, SQLite, 8 lines, as copper token blocks under its category's mask
SQLschema.sql23 linesfixed column order; sqlite3 3.51.0
PRAGMA foreign_keys = ON;  -- per connection, every time

CREATE TABLE IF NOT EXISTS blog_posts (
    id INTEGER PRIMARY KEY NOT NULL,
    title TEXT NOT NULL,
    html TEXT NOT NULL,
    parent_blog_post_id INTEGER,
    inserted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(parent_blog_post_id) REFERENCES blog_posts(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS blog_categories (
    id INTEGER PRIMARY KEY NOT NULL,
    title TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS blog_posts_categories (
    blog_post_id INTEGER NOT NULL,
    blog_category_id INTEGER NOT NULL,
    FOREIGN KEY(blog_post_id) REFERENCES blog_posts(id) ON DELETE CASCADE,
    FOREIGN KEY(blog_category_id) REFERENCES blog_categories(id) ON DELETE CASCADE,
    UNIQUE(blog_post_id, blog_category_id) ON CONFLICT REPLACE
);
  1. L1PRAGMA foreign_keys = ON; ‑‑ per connec2 lines
  2. L3CREATE TABLE IF NOT EXISTS blog_posts (7 lines
  3. L10);2 lines
  4. L12CREATE TABLE IF NOT EXISTS blog_categori3 lines
  5. L15);2 lines
  6. L17CREATE TABLE IF NOT EXISTS blog_posts_ca6 lines
  7. L23);1 line
TL;DR

ON DELETE SET NULL and ON DELETE CASCADE let SQLite clean up child and join rows itself, but only on connections that ran PRAGMA foreign_keys = ON; without it, the default, the test left two orphans and a dangling parent id. The published CREATE TABLE also failed with a syntax error until one column moved above the foreign key. Tested with sqlite3 3.51.0.

Nothing happens until you switch it on

SQLite parses ON DELETE SET NULL and ON DELETE CASCADE, but it only acts on them when PRAGMA foreign_keys = ON has been run on that connection, and every new connection starts with it off. With the default, deleting the parent post left the child pointing at it and two orphan rows in the join table. With the pragma on, both callbacks did exactly what the spell says.

After deleting the parent post: with foreign keys off, the default, the child still points at post 1 and two orphan rows stay in the join table; with foreign keys on, the child is set to NULL and the join rows cascade away, leaving 1. after deleting post 1 foreign_keys OFF foreign_keys ON child.parent_id still 1 NULL join rows 3 1 orphan join rows 2 0 After deleting the parent post: with foreign keys off, the default, the child still points at post 1 and two orphan rows stay in the join table; with foreign keys on, the child is set to NULL and the join rows cascade away, leaving 1. after deleting post 1 child.parent_id foreign_keys OFFstill 1 foreign_keys ONNULL after deleting post 1 join rows foreign_keys OFF3 foreign_keys ON1 after deleting post 1 orphan join rows foreign_keys OFF2 foreign_keys ON0
FIG. 1 The same delete with foreign keys off and on: every lit cell is a callback that only runs with the pragma.

Created, seeded, deleted, twice

As published, blog_posts did not create: SQLite wants every table constraint after the last column, and the spell put inserted_at after the foreign key. With that line moved, the test seeds a parent, a child, two categories and three links, deletes the parent and reads what is left, once with the pragma off and once on.

The runs as a rig: the published table did not create; with foreign keys off nothing cascades; with them on the child is set to NULL and the orphans are gone; the lamp reads PASS for the fixed schema with foreign keys on. published CREATE TABLEsyntax error foreign_keys OFFparent 1, 2 orphans foreign_keys ONNULL, 0 orphans verdictPASS The runs as a rig: the published table did not create; with foreign keys off nothing cascades; with them on the child is set to NULL and the orphans are gone; the lamp reads PASS for the fixed schema with foreign keys on. published CREATE TABLEsyntax error foreign_keys OFFparent 1, 2 orphans foreign_keys ONNULL, 0 orphans verdictPASS
SQLon_delete_test.sql10 linesran with foreign_keys OFF and ON
-- run once with PRAGMA foreign_keys = OFF (the default) and once with ON
INSERT INTO blog_posts (id, title, html) VALUES (1, 'Parent', '<p>p</p>');
INSERT INTO blog_posts (id, title, html, parent_blog_post_id)
  VALUES (2, 'Child', '<p>c</p>', 1);
INSERT INTO blog_categories (id, title) VALUES (10, 'Rust'), (11, 'Go');
INSERT INTO blog_posts_categories VALUES (1, 10), (1, 11), (2, 10);

DELETE FROM blog_posts WHERE id = 1;
SELECT parent_blog_post_id FROM blog_posts WHERE id = 2;
SELECT count(*) FROM blog_posts_categories;

Per connection, every time

The pragma is not stored in the database file. Each connection has to switch it on, so put it where your app opens the database: a connection hook in your driver, or the first statement after open. Both sides of the join table cascade: deleting the Rust category also removed its links. And ON CONFLICT REPLACE on the pair kept the table at three rows when I inserted an existing link again.

The pragma is per connection: a new connection reports 0, the pragma switches the callbacks on, and an app must run it each time it opens the database. a new sqlite3 connection PRAGMA foreign_keys = 0 PRAGMA foreign_keys = ON callbacks run your app: on every open run the pragma first
FIG. 2 Where the pragma has to live.

Rely on ON DELETE when

  • every connection runs PRAGMA foreign_keys = ON
  • join rows have no meaning without both sides: CASCADE
  • an optional parent may disappear: SET NULL

Be careful when

  • a tool or a shell session opens the file: the pragma is off there
  • you use INSERT OR REPLACE on a parent: it deletes and cascades
  • cascades reach far: a delete can remove more than you look at