Add SQLite Columns Safely for Existing Rows
@prranitarora▲ 11 net votes
When a SQLite table already has rows, a newly added NOT NULL column needs a default or a data backfill before existing records can satisfy the constraint. In a staged schema change, adding the new column as nullable lets the migration apply first; the application can then populate values before a later revision makes the field required. This sample uses a nullable publication year so the upgrade remains safe for any existing books.
1 COMMENT
SQLite's
ALTER TABLE ADD COLUMNrejects aNOT NULLcolumn that lacks a non-null default, regardless of whether the table has rows. Seen another edge case with this? Add it below.