Add a NOT NULL column to a SQLite table with existing rows
If SQLite reports Cannot add a NOT NULL column with default value NULL, the migration is trying to make a column required on a table that already contains rows. Add a non-NULL default if that value should also apply to future inserts that omit the column. If old rows need different values or future writes must supply a value, backfill the column first, then enforce NOT NULL.
A fresh database can hide this bug. An AI coding agent might update the CREATE TABLE statement and pass a test that initializes the database from scratch. An upgrade starts with the previous schema and data, so it needs its own test.
Reproduce the SQLite error on a populated table
Run this setup in a scratch SQLite database. Use a fresh copy of this fixture for each migration option below.
CREATE TABLE tasks (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
is_public INTEGER NOT NULL CHECK (is_public IN (0, 1))
);
CREATE INDEX tasks_title_idx ON tasks (title);
INSERT INTO tasks (id, title, is_public) VALUES
(1, 'Write release notes', 1),
(2, 'Draft next sprint', 0);
Now run the migration that fails on this fixture.
ALTER TABLE tasks ADD COLUMN visibility TEXT NOT NULL;
The two rows have no value for visibility. When SQLite adds a plain column with a constant default, it can update the schema without rewriting old rows. Those rows read back with the default, and future inserts that omit the column get the same value. SQLite ADD COLUMN documentation
Use a default when the same value fits old and new rows
Suppose every task should start private, including old tasks, and a caller may leave that choice implicit. Use a permanent default.
ALTER TABLE tasks
ADD COLUMN visibility TEXT NOT NULL DEFAULT 'private';
Both fixture rows now read private. An insert with an explicit NULL still fails. If task 1 should stay public because is_public is 1, use the backfill below. A made-up default would make the migration pass while changing the meaning of that row.
Backfill, then enforce NOT NULL on SQLite 3.53 or newer
Check the SQLite version through the same connection your app or migration runner uses. Its driver may bundle a different SQLite library than the terminal's sqlite3 command.
SELECT sqlite_version();
SQLite 3.53.0 added the ability to set NOT NULL on an existing column. The syntax is ALTER COLUMN visibility SET NOT NULL. This example preserves the old public flag and leaves the new column without a default, so future inserts must supply it. SQLite 3.53.0 release notes
BEGIN IMMEDIATE;
ALTER TABLE tasks ADD COLUMN visibility TEXT;
UPDATE tasks
SET visibility = CASE is_public
WHEN 1 THEN 'public'
WHEN 0 THEN 'private'
END
WHERE visibility IS NULL;
ALTER TABLE tasks ALTER COLUMN visibility SET NOT NULL;
COMMIT;
The old CHECK and NOT NULL constraints on is_public give the update a rule for every fixture row. For a rule that can leave rows unresolved, count the remaining NULL values before setting the constraint and stop the migration if any remain. Have your migration runner roll back on the first error. If it already opens a transaction, use that transaction and omit the explicit BEGIN and COMMIT. SQLite transaction behavior
Rebuild the table on older SQLite versions
For this fixture on SQLite before 3.53, create the new table with the final schema and compute each value while copying the rows. The example has one index and no foreign keys, triggers, or views.
BEGIN IMMEDIATE;
CREATE TABLE tasks_new (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
is_public INTEGER NOT NULL CHECK (is_public IN (0, 1)),
visibility TEXT NOT NULL
);
INSERT INTO tasks_new (id, title, is_public, visibility)
SELECT id, title, is_public,
CASE is_public
WHEN 1 THEN 'public'
WHEN 0 THEN 'private'
END
FROM tasks;
DROP TABLE tasks;
ALTER TABLE tasks_new RENAME TO tasks;
CREATE INDEX tasks_title_idx ON tasks (title);
COMMIT;
For your own table, preserve its constraints, indexes, triggers, and dependent views. If foreign keys are involved, follow SQLite's documented rebuild procedure. It changes foreign-key enforcement before opening the transaction and runs PRAGMA foreign_key_check before commit. Treat returned violations as a migration failure. SQLite’s table rebuild procedure
Test the upgrade and the next insert
After either backfill, run these queries.
SELECT id, visibility FROM tasks ORDER BY id;
PRAGMA table_info(tasks);
PRAGMA index_list(tasks);
Expect 1 | public and 2 | private. In table_info, visibility should have notnull set to 1 and dflt_value set to SQL NULL, which means there is no declared default. The index list should still contain tasks_title_idx. The permanent-default option instead gives both rows private and reports the declared default.
Test these inserts separately on the migrated scratch database.
INSERT INTO tasks (id, title, is_public, visibility)
VALUES (3, 'Plan launch', 0, 'private');
INSERT INTO tasks (id, title, is_public)
VALUES (4, 'Missing visibility', 0);
INSERT INTO tasks (id, title, is_public, visibility)
VALUES (5, 'Null visibility', 0, NULL);
After either backfill, the first insert should succeed and the next two should fail with NOT NULL constraint failed: tasks.visibility. With a permanent default, the second insert succeeds and stores private. The explicit NULL still fails. Keep these checks in an upgrade test that starts from the old schema and invokes your actual migration runner.
Check deployed writers before enforcing the constraint. An old worker that omits visibility will start failing on the no-default schema. If workers must stay up, first add the nullable column, deploy writers that fill it, and confirm the old writers are gone. Then backfill any remaining rows and enforce the constraint.
When handing the fix back to a coding agent, give it the old schema, the two fixture rows, and the expected results. Ask it to test both an upgrade and a new install with the SQLite version used in deployment. Testing AI-generated code