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
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.
官方文档
https://developer.android.com/training/data-storage/room/migrating解决方案
-
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.
-
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). -
Export the schema JSON files by setting room.schemaLocation in build.gradle and use them to generate migration test cases.
无效尝试
常见但无效的做法:
-
90% 失败
Uninstalling the app and reinstalling works temporarily but loses user data, unacceptable for production.
-
80% 失败
Setting fallbackToDestructiveMigration() in the database builder causes data loss on every schema change, which is destructive.
-
95% 失败
Manually altering the database version number without providing a migration leads to the same error.