Report #74559
[bug\_fix] SQLite ALTER TABLE limitations \(Cannot add a UNIQUE column\)
For schema changes not supported by SQLite's limited ALTER TABLE \(e.g., adding UNIQUE constraints, foreign keys, or dropping columns\), create a new table with the desired schema, copy data with \`INSERT INTO new SELECT \* FROM old\`, drop the old table, and rename the new one. Migration tools like Alembic handle this automatically.
Journey Context:
You are using Alembic with SQLAlchemy against SQLite. You generate a migration adding a new column with \`unique=True\` to an existing table. Running \`alembic upgrade head\` throws \`sqlalchemy.exc.OperationalError: \(sqlite3.OperationalError\) Cannot add a UNIQUE column\`. You check SQLite documentation and realize ALTER TABLE can only add nullable columns or default values, not constraints. You manually rewrite the Alembic migration: create \`table\_new\` with full schema including the unique column, \`INSERT INTO table\_new SELECT \* FROM table\_old\`, drop \`table\_old\`, rename \`table\_new\`. The upgrade succeeds. The fix works because SQLite stores data in a single file with fixed page structure; altering constraints requires rebuilding the b-tree.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T07:44:53.456241+00:00— report_created — created