{"record":{"id":"f05ee4f91a00443c","repo":"invoke-ai/InvokeAI","slug":"database-contains-inconsistent-applied-migration-s-f05ee4","errorCode":null,"errorMessage":"Database contains inconsistent applied migration state: legacy version {legacy_version} is recorded for {legacy_row[0]}, expected {migration_id}","messagePattern":"Database contains inconsistent applied migration state: legacy version (.+?) is recorded for (.+?), expected (.+?)","errorType":"exception","errorClass":"MigrationError","httpStatus":null,"severity":"critical","filePath":"invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py","lineNumber":195,"sourceCode":"                    \"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),\n                )\n            cursor.connection.commit()\n        except sqlite3.Error as e:\n            msg = f\"Problem bootstrapping applied migrations: {e}\"\n            self._logger.error(msg)\n            cursor.connection.rollback()\n            raise MigrationError(msg) from e\n\n    def _validate_existing_applied_migrations(self, cursor: sqlite3.Cursor) -> None:\n        \"\"\"Validates existing applied migration IDs before creating or mutating migrator metadata.\"\"\"\n        applied_migration_ids = self._get_applied_migration_ids(cursor=cursor)\n        if len(applied_migration_ids) == 0:","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py#L177-L213","documentation":"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.","triggerScenarios":"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.","commonSituations":"Corrupted or duplicated rows in applied_migrations; concurrent migrator runs against the same DB; manual edits or merges of the DB file.","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"],"exampleFix":"# find the conflicting row\nsqlite3 invokeai.db \"SELECT * FROM applied_migrations WHERE legacy_version=7;\"\n# before: legacy_version 7 recorded for wrong migration\n# after: remove the wrong row, then rerun migrations\nsqlite3 invokeai.db \"DELETE FROM applied_migrations WHERE legacy_version=7 AND migration_id != 'migration_7';\"","handlingStrategy":"validation","validationCode":"import sqlite3\nrows = sqlite3.connect(db_path).execute(\n    \"SELECT legacy_version, migration_id FROM applied_migrations\"\n).fetchall()\nseen = {}\nfor lv, mid in rows:\n    if lv in seen and seen[lv] != mid:\n        raise RuntimeError(f\"legacy_version {lv} claimed by multiple migrations\")\n    seen[lv] = mid","typeGuard":null,"tryCatchPattern":"try:\n    services.run_migrations()\nexcept MigrationError as e:\n    if \"is recorded for\" in str(e):\n        logger.critical(\"Duplicate legacy_version rows in applied_migrations: %s\", e)\n        raise\n    raise","preventionTips":["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"],"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"}