SQLite On Delete Callbacks
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
);
- L1
PRAGMA foreign_keys = ON; ‑‑ per connec2 lines - L3
CREATE TABLE IF NOT EXISTS blog_posts (7 lines - L10
);2 lines - L12
CREATE TABLE IF NOT EXISTS blog_categori3 lines - L15
);2 lines - L17
CREATE TABLE IF NOT EXISTS blog_posts_ca6 lines - L23
);1 line
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.
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.
-- 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.
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 REPLACEon a parent: it deletes and cascades - cascades reach far: a delete can remove more than you look at

