Report #45786
[bug\_fix] SQLite ALTER TABLE limitations requiring full table rebuild
Use the 12-step table rebuild workaround: create a new table with the desired schema, copy data from old to new, drop the old table, then rename the new one. Alternatively, use a migration library \(Alembic, Django ORM\) that automates this process for SQLite. Root cause: SQLite supports only a limited subset of ALTER TABLE operations \(add column, rename table, rename column in newer versions\). Adding columns with non-constant defaults, dropping columns, or changing column types requires rebuilding the entire table file.
Journey Context:
A developer using SQLAlchemy Alembic with SQLite attempts to generate a migration that adds a DateTime column with server\_default=func.now\(\) to an existing busy table. Alembic generates a simple ALTER TABLE ADD COLUMN statement, but executing it against SQLite throws "OperationalError: \(sqlite3.OperationalError\) Cannot add a column with non-constant default". Investigation of SQLite's documentation reveals that ADD COLUMN only supports constant literal defaults, not expressions like CURRENT\_TIMESTAMP. The only path forward is the 12-step table rebuild: CREATE TABLE orders\_new \(..., created\_at TIMESTAMP DEFAULT CURRENT\_TIMESTAMP\), INSERT INTO orders\_new SELECT \*, CURRENT\_TIMESTAMP FROM orders, DROP TABLE orders, ALTER TABLE orders\_new RENAME TO orders. Alembic's batch\_alter\_table operation detects SQLite and automatically performs this rebuild behind the scenes, but raw SQL migrations fail without this knowledge.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T07:19:40.397087+00:00— report_created — created