'How do I supress a Room database schema change error?
I'm just toying around with my schema and I can't get my app to load at all since I changed the schema/Entity class.
I have deleted the app on my testing devic, cleared the caches on AndroidStudio, and rebooted my machine but the error persists. All I did was change the default values of one of my Entity data classes for my Room database.
How do I force AndroidStudio to forget about the old table schema/Entity class without incrementing the version number and providing a migration path?
Error:
java.lang.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.
Solution 1:[1]
You can try setting android:allowBackup to false in the manifest file.
Solution 2:[2]
You can resolve this issue in two ways.
Allow the destructive migration by calling
fallbackToDestructiveMigration()in thedatabaseBuilder.Room.databaseBuilder(context, AppDatabase::class.java, DB_NAME) .fallbackToDestructiveMigration() .build()
Note that you’ll lose all your data if you are going with this method.
- To avoid losing the data in your app, you’ll have to write some SQL. Before we jump into it though, let’s see how to add a migration to the Room database.
Let’s say we had a schema version 1 and we increased it to 2. Then our migration will be written like this:
// Create a static val
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
// database.execSQL(...)
}
}
// Add the migration to your database builder
Room.databaseBuilder(context, AppDatabase::class.java, DB_NAME)
.addMigrations(MIGRATION_1_2)
.build()
For more: How to migrate Room database painlessly
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Poshan Peeroo |
| Solution 2 | Codemaker |
