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

- **ID:** `android/illegalstateexception-room-migration-failed`
- **领域:** android
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

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

## 解决方案

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.
   ```

## 无效尝试

- **** — Uninstalling the app and reinstalling works temporarily but loses user data, unacceptable for production. (90% 失败率)
- **** — Setting fallbackToDestructiveMigration() in the database builder causes data loss on every schema change, which is destructive. (80% 失败率)
- **** — Manually altering the database version number without providing a migration leads to the same error. (95% 失败率)
