{"record":{"id":"5eee1c67ae77d555","repo":"invoke-ai/InvokeAI","slug":"database-contains-inconsistent-applied-migration-s","errorCode":null,"errorMessage":"Database contains inconsistent applied migration state: {migration_id} is recorded with legacy version {recorded}, expected {expected}","messagePattern":"Database contains inconsistent applied migration state: (.+?) is recorded with legacy version (.+?), expected (.+?)","errorType":"exception","errorClass":"MigrationError","httpStatus":null,"severity":"critical","filePath":"invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py","lineNumber":183,"sourceCode":"    def _bootstrap_applied_migrations_from_legacy_versions(self, cursor: sqlite3.Cursor) -> None:\n        \"\"\"Backfills applied migration IDs from legacy numeric migration rows.\"\"\"\n        try:\n            cursor.execute(\"SELECT version FROM migrations WHERE version > 0 ORDER BY version;\")\n            legacy_versions = [row[0] for row in cursor.fetchall()]\n            registered_migration_ids = self._migration_set.migrations_by_id\n            for legacy_version in legacy_versions:\n                migration_id = f\"migration_{legacy_version}\"\n                if migration_id not in registered_migration_ids:\n                    cursor.connection.rollback()\n                    raise MigrationError(f\"Database contains unknown legacy migration version: {legacy_version}\")\n                cursor.execute(\n                    \"SELECT legacy_version FROM applied_migrations WHERE migration_id = ?;\",\n                    (migration_id,),\n                )\n                migration_row = cursor.fetchone()\n                if migration_row is not None and migration_row[0] != legacy_version:\n                    cursor.connection.rollback()\n                    raise MigrationError(\n                        \"Database contains inconsistent applied migration state: \"\n                        f\"{migration_id} is recorded with legacy version {migration_row[0]}, \"\n                        f\"expected {legacy_version}\"\n                    )\n                cursor.execute(\n                    \"SELECT migration_id FROM applied_migrations WHERE legacy_version = ?;\",\n                    (legacy_version,),\n                )\n                legacy_row = cursor.fetchone()\n                if legacy_row is not None and legacy_row[0] != migration_id:\n                    cursor.connection.rollback()\n                    raise MigrationError(\n                        \"Database contains inconsistent applied migration state: \"\n                        f\"legacy version {legacy_version} is recorded for {legacy_row[0]}, expected {migration_id}\"\n                    )\n                cursor.execute(\n                    \"INSERT OR IGNORE INTO applied_migrations (migration_id, legacy_version) VALUES (?, ?);\",\n                    (migration_id, legacy_version),","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py#L165-L201","documentation":"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.","triggerScenarios":"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.","commonSituations":"A previous bootstrap crashed between inserts; someone hand-edited applied_migrations; DB copied/synced mid-migration leaving mixed rows.","solutions":["Restore a DB backup from before the inconsistent bootstrap","Manually reconcile the applied_migrations rows (delete conflicting rows) and rerun migrations","If data is disposable, recreate the database"],"exampleFix":"# inspect the conflicting row\nsqlite3 invokeai.db \"SELECT * FROM applied_migrations WHERE migration_id='migration_12';\"\n# before: inconsistent legacy_version on the row\n# after: fix or delete the row, then rerun\nsqlite3 invokeai.db \"DELETE FROM applied_migrations WHERE migration_id='migration_12';\"","handlingStrategy":"validation","validationCode":"import sqlite3\nrows = sqlite3.connect(db_path).execute(\n    \"SELECT migration_id, legacy_version FROM applied_migrations\"\n).fetchall()\nseen = {}\nfor mid, lv in rows:\n    if mid in seen and seen[mid] != lv:\n        raise RuntimeError(f\"inconsistent applied_migrations row for {mid}\")\n    seen[mid] = lv","typeGuard":null,"tryCatchPattern":"try:\n    services.run_migrations()\nexcept MigrationError as e:\n    if \"inconsistent applied migration state\" in str(e):\n        logger.critical(\"Reconcile applied_migrations rows: %s\", e)\n        raise\n    raise","preventionTips":["Never hand-edit the applied_migrations table","Avoid interrupting bootstrap mid-transaction (it should roll back, but verify)","Restore a clean backup rather than patching inconsistent rows ad hoc"],"tags":["database","migrations","consistency"],"backgroundTag":"inconsistent-migration-state","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}