invoke-ai/InvokeAI · critical · MigrationError

Database contains inconsistent applied migration state: {mig

Error message

Database contains inconsistent applied migration state: {migration_id} is recorded with legacy version {recorded}, expected {expected}

What it means

During legacy bootstrap, if applied_migrations already contains a row for migration_{version} but with a different legacy_version value recorded, the two sources of truth disagree. The bootstrap rolls back and raises MigrationError describing the recorded vs expected legacy version.

Source

Thrown at invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py:183

    def _bootstrap_applied_migrations_from_legacy_versions(self, cursor: sqlite3.Cursor) -> None:
        """Backfills applied migration IDs from legacy numeric migration rows."""
        try:
            cursor.execute("SELECT version FROM migrations WHERE version > 0 ORDER BY version;")
            legacy_versions = [row[0] for row in cursor.fetchall()]
            registered_migration_ids = self._migration_set.migrations_by_id
            for legacy_version in legacy_versions:
                migration_id = f"migration_{legacy_version}"
                if migration_id not in registered_migration_ids:
                    cursor.connection.rollback()
                    raise MigrationError(f"Database contains unknown legacy migration version: {legacy_version}")
                cursor.execute(
                    "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),

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Restore a DB backup from before the inconsistent bootstrap
  2. Manually reconcile the applied_migrations rows (delete conflicting rows) and rerun migrations
  3. If data is disposable, recreate the database

Example fix

# inspect the conflicting row
sqlite3 invokeai.db "SELECT * FROM applied_migrations WHERE migration_id='migration_12';"
# before: inconsistent legacy_version on the row
# after: fix or delete the row, then rerun
sqlite3 invokeai.db "DELETE FROM applied_migrations WHERE migration_id='migration_12';"
Defensive patterns

Strategy: validation

Validate before calling

import sqlite3
rows = sqlite3.connect(db_path).execute(
    "SELECT migration_id, legacy_version FROM applied_migrations"
).fetchall()
seen = {}
for mid, lv in rows:
    if mid in seen and seen[mid] != lv:
        raise RuntimeError(f"inconsistent applied_migrations row for {mid}")
    seen[mid] = lv

Try / catch

try:
    services.run_migrations()
except MigrationError as e:
    if "inconsistent applied migration state" in str(e):
        logger.critical("Reconcile applied_migrations rows: %s", e)
        raise
    raise

Prevention

When it happens

Trigger: Legacy version table says version V maps to migration_N, but applied_migrations already has migration_N recorded with legacy_version != V — e.g. partially or inconsistently applied bootstrap from a previous failed run or manual edits.

Common situations: A previous bootstrap crashed between inserts; someone hand-edited applied_migrations; DB copied/synced mid-migration leaving mixed rows.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/5eee1c67ae77d555. Report an issue: GitHub.