Report #9035
[bug\_fix] SQLite OperationalError: table X already exists
Make migration scripts idempotent by using CREATE TABLE IF NOT EXISTS instead of bare CREATE TABLE, or use a proper migration framework \(Alembic, Django ORM, Flyway\) that tracks applied migrations in a schema history table. If a crash left the database in a half-migrated state, manually reconcile the schema\_migrations table with the actual schema in sqlite\_master.
Journey Context:
A developer is iterating on a Python CLI tool using SQLite for local state. They write a manual SQL migration script 001\_initial.sql containing CREATE TABLE users \(...\). During development, the script crashes halfway through after creating the users table but before the app records that migration 001 ran. Rerunning the app throws OperationalError: table users already exists. The developer considers dropping the database file entirely, but that loses data. Realizing the migration isn't idempotent, they modify the script to use CREATE TABLE IF NOT EXISTS users \(...\) which allows rerunning safely. For production scenarios, they switch to using Alembic which tracks versions in an alembic\_version table, ensuring partial failures don't leave the system in an inconsistent state requiring manual intervention on sqlite\_master.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:10:35.903630+00:00— report_created — created