android runtime_error ai_generated true

IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the migration(s).

ID: android/illegalstateexception-room-migration-failed

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2023-09-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Room 2.5.2 active
Room 2.6.0 active
Android 13 (API 33) active
Android 14 (API 34) active

Root Cause

Database schema version mismatch: the code expects a newer schema than the installed database, and no migration path is provided.

generic

中文

数据库架构版本不匹配:代码期望的架构比已安装的数据库新,且未提供迁移路径。

Official Documentation

https://developer.android.com/training/data-storage/room/migrating

Workarounds

  1. 90% success Add a Room migration from the old version to the new version. Example: Room.databaseBuilder(context, AppDatabase.class, "my-db").addMigrations(MIGRATION_1_2).build(); where MIGRATION_1_2 is a Migration object that executes ALTER TABLE statements.
    Add a Room migration from the old version to the new version. Example: Room.databaseBuilder(context, AppDatabase.class, "my-db").addMigrations(MIGRATION_1_2).build(); where MIGRATION_1_2 is a Migration object that executes ALTER TABLE statements.
  2. 80% success Use AutoMigration with @Database(version = 2, autoMigrations = {@AutoMigration(from = 1, to = 2)}) if schema changes are simple (e.g., adding a column with a default value).
    Use AutoMigration with @Database(version = 2, autoMigrations = {@AutoMigration(from = 1, to = 2)}) if schema changes are simple (e.g., adding a column with a default value).
  3. 75% success Export the schema JSON files by setting room.schemaLocation in build.gradle and use them to generate migration test cases.
    Export the schema JSON files by setting room.schemaLocation in build.gradle and use them to generate migration test cases.

中文步骤

  1. Add a Room migration from the old version to the new version. Example: Room.databaseBuilder(context, AppDatabase.class, "my-db").addMigrations(MIGRATION_1_2).build(); where MIGRATION_1_2 is a Migration object that executes ALTER TABLE statements.
  2. Use AutoMigration with @Database(version = 2, autoMigrations = {@AutoMigration(from = 1, to = 2)}) if schema changes are simple (e.g., adding a column with a default value).
  3. Export the schema JSON files by setting room.schemaLocation in build.gradle and use them to generate migration test cases.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Uninstalling the app and reinstalling works temporarily but loses user data, unacceptable for production.

  2. 80% fail

    Setting fallbackToDestructiveMigration() in the database builder causes data loss on every schema change, which is destructive.

  3. 95% fail

    Manually altering the database version number without providing a migration leads to the same error.