invoke-ai/InvokeAI · critical · MigrationError
Database contains inconsistent applied migration state: lega
Error message
Database contains inconsistent applied migration state: legacy version {legacy_version} is recorded for {legacy_row[0]}, expected {migration_id} What it means
Symmetric consistency check to [827]: after confirming migration_id maps to the legacy version, the code looks for any row claiming that legacy_version for a different migration id. If found, it rolls back and raises MigrationError stating which migration id the legacy version is recorded for and which was expected.
Source
Thrown at invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py:195
"SELECT legacy_version FROM applied_migrations WHERE migration_id = ?;",
(migration_id,),
)
migration_row = cursor.fetchone()
if migration_row is not None and migration_row[0] != legacy_version:
cursor.connection.rollback()
raise MigrationError(
"Database contains inconsistent applied migration state: "
f"{migration_id} is recorded with legacy version {migration_row[0]}, "
f"expected {legacy_version}"
)
cursor.execute(
"SELECT migration_id FROM applied_migrations WHERE legacy_version = ?;",
(legacy_version,),
)
legacy_row = cursor.fetchone()
if legacy_row is not None and legacy_row[0] != migration_id:
cursor.connection.rollback()
raise MigrationError(
"Database contains inconsistent applied migration state: "
f"legacy version {legacy_version} is recorded for {legacy_row[0]}, expected {migration_id}"
)
cursor.execute(
"INSERT OR IGNORE INTO applied_migrations (migration_id, legacy_version) VALUES (?, ?);",
(migration_id, legacy_version),
)
cursor.connection.commit()
except sqlite3.Error as e:
msg = f"Problem bootstrapping applied migrations: {e}"
self._logger.error(msg)
cursor.connection.rollback()
raise MigrationError(msg) from e
def _validate_existing_applied_migrations(self, cursor: sqlite3.Cursor) -> None:
"""Validates existing applied migration IDs before creating or mutating migrator metadata."""
applied_migration_ids = self._get_applied_migration_ids(cursor=cursor)
if len(applied_migration_ids) == 0:View on GitHub (pinned to 0b6a024f2f)
Solutions
- Restore a DB backup taken before the corruption
- Delete or correct the conflicting legacy_version row in applied_migrations and rerun
- Recreate the database if its history cannot be reconciled
Example fix
# find the conflicting row sqlite3 invokeai.db "SELECT * FROM applied_migrations WHERE legacy_version=7;" # before: legacy_version 7 recorded for wrong migration # after: remove the wrong row, then rerun migrations sqlite3 invokeai.db "DELETE FROM applied_migrations WHERE legacy_version=7 AND migration_id != 'migration_7';"
Defensive patterns
Strategy: validation
Validate before calling
import sqlite3
rows = sqlite3.connect(db_path).execute(
"SELECT legacy_version, migration_id FROM applied_migrations"
).fetchall()
seen = {}
for lv, mid in rows:
if lv in seen and seen[lv] != mid:
raise RuntimeError(f"legacy_version {lv} claimed by multiple migrations")
seen[lv] = mid Try / catch
try:
services.run_migrations()
except MigrationError as e:
if "is recorded for" in str(e):
logger.critical("Duplicate legacy_version rows in applied_migrations: %s", e)
raise
raise Prevention
- Run only one InvokeAI migrator at a time against a DB
- Check applied_migrations for duplicate legacy_version values after restoring backups
- Treat the table as migrator-owned; do not merge DB files manually
When it happens
Trigger: applied_migrations contains a row where legacy_version = V belongs to migration id X, but the legacy tables say V should map to migration_{V} — duplicate/conflicting legacy_version entries in the table.
Common situations: Corrupted or duplicated rows in applied_migrations; concurrent migrator runs against the same DB; manual edits or merges of the DB file.
Related errors
- Database contains inconsistent applied migration state: {mig
- Image move job {job_id} failed commit validation
- Database contains unknown applied migration IDs: {unknown_id
- Database is at version {version}, expected {expected}
- Database contains unknown legacy migration version: {legacy_
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/f05ee4f91a00443c.
Report an issue: GitHub.