Report #13116
[bug\_fix] SQLITE\_CONSTRAINT\_FOREIGNKEY during ALTER TABLE
Temporarily disable foreign key enforcement \(PRAGMA foreign\_keys = OFF\), execute the ALTER TABLE statement, then re-enable \(PRAGMA foreign\_keys = ON\) and verify integrity with PRAGMA foreign\_key\_check; alternatively, use newer non-rebuilding ALTER TABLE syntax like RENAME COLUMN \(SQLite >=3.25.0\) or DROP COLUMN \(>=3.35.0\) which do not violate foreign keys. Root cause: SQLite's legacy ALTER TABLE implementation creates a temporary table, copies data, drops the original, and renames the temp table; during this transition, foreign key constraints referencing or originating from the altered table are temporarily violated because the referenced table appears missing or in an inconsistent state.
Journey Context:
You're deploying a schema migration to an embedded SQLite database in a mobile app, adding a new column to the 'customers' table. The migration script executes 'ALTER TABLE customers ADD COLUMN tier TEXT'. It crashes with 'SQLITE\_CONSTRAINT\_FOREIGNKEY: foreign key constraint failed'. You inspect the schema and see that the 'orders' table has a FOREIGN KEY \(customer\_id\) REFERENCES customers\(id\). You realize SQLite implements ALTER TABLE by rebuilding the entire table, and during the brief moment when the old 'customers' table is dropped but the new one hasn't been renamed, the foreign key constraint from 'orders' is violated because it references a table that temporarily doesn't exist. You modify the migration script to first execute 'PRAGMA foreign\_keys=OFF', run the ALTER TABLE, then 'PRAGMA foreign\_keys=ON', followed by 'PRAGMA foreign\_key\_check' to verify no actual referential integrity violations occurred during the migration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T17:48:19.133393+00:00— report_created — created