{"record":{"id":"b5ead3c67486e395","repo":"invoke-ai/InvokeAI","slug":"database-contains-unknown-applied-migration-ids-b5ead3","errorCode":null,"errorMessage":"Database contains unknown applied migration IDs: {unknown_ids}","messagePattern":"Database contains unknown applied migration IDs: (.+?)","errorType":"exception","errorClass":"MigrationError","httpStatus":null,"severity":"error","filePath":"invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py","lineNumber":219,"sourceCode":"                    (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:\n            return\n        known_migration_ids = set(self._migration_set.migrations_by_id)\n        unknown_applied_ids = applied_migration_ids - known_migration_ids\n        if unknown_applied_ids:\n            unknown_ids = \", \".join(sorted(unknown_applied_ids))\n            raise MigrationError(f\"Database contains unknown applied migration IDs: {unknown_ids}\")\n\n    def _validate_existing_legacy_migrations(self, cursor: sqlite3.Cursor) -> None:\n        \"\"\"Validates existing legacy migration versions before creating applied migration metadata.\"\"\"\n        try:\n            cursor.execute(\"SELECT version FROM migrations WHERE version > 0 ORDER BY version;\")\n        except sqlite3.OperationalError as e:\n            if \"no such table\" in str(e):\n                return\n            raise\n\n        registered_migration_ids = self._migration_set.migrations_by_id\n        for row in cursor.fetchall():\n            legacy_version = row[0]\n            migration_id = f\"migration_{legacy_version}\"\n            if migration_id not in registered_migration_ids:\n                raise MigrationError(f\"Database contains unknown legacy migration version: {legacy_version}\")\n\n    def _validate_existing_applied_legacy_migrations(self, cursor: sqlite3.Cursor) -> None:","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_impl.py#L201-L237","documentation":"The SQLite migrator found migration IDs recorded in the database's applied_migrations table that are not registered in the MigrationSet the app was built with. This means the database was created or modified by a different (usually newer) version of the application than the one currently running. It aborts startup rather than silently running migrations over unknown state.","triggerScenarios":"Calling run_migrations() when the DB's applied_migrations table contains migration IDs absent from self._migration_set.migrations_by_id (unknown_applied_ids non-empty after set subtraction).","commonSituations":"User downgraded InvokeAI to an older release after a newer version migrated the DB; running two different app versions against the same database file; a custom/branch build added migrations then the user switched to stock.","solutions":["Upgrade the application back to the version that created those migrations (matching or newer than the DB).","Restore the database from a backup taken before the newer version ran.","If the downgrade is intentional and safe, manually delete the unknown rows from applied_migrations only after confirming the schema changes are compatible (risk: data/schema corruption)."],"exampleFix":"// before: running InvokeAI 5.x against a DB migrated by 6.x\n# pip install invokeai==5.3.0\n// after: install the version matching the DB\n# pip install invokeai==6.0.1\n# invokeai-configure --version  # then restart; run_migrations() passes validation","handlingStrategy":"try-catch","validationCode":"// before launching the app / running migrations\nimport sqlite3\nconn = sqlite3.connect(db_path)\napplied = {r[0] for r in conn.execute(\"SELECT migration_id FROM applied_migrations\")}\nprint(sorted(applied))  # compare against the MigrationSet of your app version;\n                        # if IDs look newer than your install, upgrade first","typeGuard":null,"tryCatchPattern":"from invokeai.app.services.shared.sqlite_migrator.sqlite_migrator_common import MigrationError\ntry:\n    migrator.run_migrations()\nexcept MigrationError as e:\n    if \"unknown applied migration IDs\" in str(e):\n        # stop and upgrade the app or restore a backup; do not auto-retry\n        raise SystemExit(f\"DB was migrated by a newer version: {e}\")\n    raise","preventionTips":["Always back up the SQLite database before upgrading InvokeAI.","Never run an older app version against a DB migrated by a newer one.","Avoid sharing one database file across multiple app versions/installs.","Check release notes for schema migrations before downgrading."],"tags":["database","sqlite","migration","version-mismatch"],"backgroundTag":"unknown-applied-migration","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}