android data_error ai_generated true

IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

ID: android/room-migration-not-found

Also available as: JSON · Markdown · 中文
87%Fix Rate
89%Confidence
1Evidence
2023-12-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Room 2.4.0 active
Room 2.5.0 active
Room 2.6.0 active

Root Cause

Room database schema has changed (e.g., added column) but version number not incremented or migration not provided, causing integrity check failure.

generic

中文

Room 数据库架构已更改(例如添加了列),但版本号未增加或未提供迁移,导致完整性检查失败。

Official Documentation

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

Workarounds

  1. 90% success Increment database version and add migration: `@Database(entities = [User::class], version = 2)` and `val MIGRATION_1_2 = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE User ADD COLUMN age INTEGER NOT NULL DEFAULT 0") } }`
    Increment database version and add migration: `@Database(entities = [User::class], version = 2)` and `val MIGRATION_1_2 = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE User ADD COLUMN age INTEGER NOT NULL DEFAULT 0") } }`
  2. 85% success Use `autoMigrations` annotation: `@Database(entities = [User::class], version = 2, autoMigrations = [AutoMigration(from = 1, to = 2)])` for simple changes like adding columns with defaults.
    Use `autoMigrations` annotation: `@Database(entities = [User::class], version = 2, autoMigrations = [AutoMigration(from = 1, to = 2)])` for simple changes like adding columns with defaults.
  3. 80% success Export schema to JSON using `room.schemaLocation` in build.gradle, then use Room's automatic migration validator to detect issues.
    Export schema to JSON using `room.schemaLocation` in build.gradle, then use Room's automatic migration validator to detect issues.

中文步骤

  1. Increment database version and add migration: `@Database(entities = [User::class], version = 2)` and `val MIGRATION_1_2 = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE User ADD COLUMN age INTEGER NOT NULL DEFAULT 0") } }`
  2. Use `autoMigrations` annotation: `@Database(entities = [User::class], version = 2, autoMigrations = [AutoMigration(from = 1, to = 2)])` for simple changes like adding columns with defaults.
  3. Export schema to JSON using `room.schemaLocation` in build.gradle, then use Room's automatic migration validator to detect issues.

Dead Ends

Common approaches that don't work:

  1. Delete app data or uninstall app 60% fail

    Loses all user data; only works for development, not production.

  2. Set fallbackToDestructiveMigration() in Room database builder 75% fail

    Destroys existing database on version mismatch, causing data loss; not suitable for production.

  3. Ignore the error and continue 95% fail

    App crashes on database access; cannot proceed without fixing migration.