Report #103421
[bug\_fix] SQLite migration fails to alter a column or add a NOT NULL column without default
Use SQLite's table-rebuild pattern: CREATE TABLE new\_table \(...\), INSERT INTO new\_table SELECT ... FROM old\_table, DROP TABLE old\_table, ALTER TABLE new\_table RENAME TO old\_table. Run it inside a single transaction. Use a framework migration tool that abstracts this \(e.g., Alembic's batch\_alter\_table\) instead of hand-writing fragile ALTER statements.
Journey Context:
A migration to add a non-nullable column to an existing SQLite table failed with Cannot add a NOT NULL column with default value NULL. SQLite's ALTER TABLE supports only a small set of operations: renaming the table, adding a column \(with restrictions\), and renaming a column in newer versions. It cannot add a NOT NULL column without a default, change a column type, or drop a column directly. The first attempt to run multiple ALTER statements hit the limits one after another. The working approach was to create a new table with the desired schema, copy the data, drop the old table, and rename. Using Alembic's batch\_alter\_table did the same rebuild under the hood and made the migration portable. The migration then ran cleanly because SQLite's storage engine is perfectly happy with the rebuilt table; the limitation is only in the ALTER TABLE statement grammar.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:22:15.574635+00:00— report_created — created