android runtime_error ai_generated true

IllegalStateException: Room 无法验证数据完整性。看起来您更改了架构但忘记更新迁移。

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

其他格式: JSON · Markdown 中文 · English
85%修复率
88%置信度
1证据数
2023-09-10首次发现

版本兼容性

版本状态引入弃用备注
Room 2.5.2 active
Room 2.6.0 active
Android 13 (API 33) active
Android 14 (API 34) active

根因分析

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

English

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

generic

官方文档

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

解决方案

  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.

无效尝试

常见但无效的做法:

  1. 90% 失败

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

  2. 80% 失败

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

  3. 95% 失败

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