Room cannot verify the data integrity

前端 未结 20 2272
一向
一向 2020-12-04 11:43

I am getting this error while running program with Room Database

Room cannot verify the data integrity. Looks like you\'ve changed schema but forgot to updat         


        
相关标签:
20条回答
  • 2020-12-04 12:28

    In my case i was using a transaction inside the migration and Room could not update the hash using a Migration Helper

    @get:Rule
    val migrationTestHelper: MigrationTestHelper =
    
    MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
                    C2GDatabase::class.java.canonicalName,
                    FrameworkSQLiteOpenHelperFactory()) 
    /* Testing method throws error*/
    db = migrationTestHelper.runMigrationsAndValidate(C2GDatabase.DB_NAME,
                3,
                false,
                C2GDatabase.Migration_1_2(),
                C2GDatabase.Migration_2_3())
    
    
    override fun migrate(database: SupportSQLiteDatabase) {
    
    /** 
        Error
        database.beginTransaction()
    **/
    database.execSQL("PRAGMA foreign_keys=off;")
    database.execSQL("ALTER TABLE user RENAME TO user_old;")
    database.execSQL("CREATE TABLE user ( id_user INTEGER PRIMARY KEY AUTOINCREMENT, external_id INTEGER NOT NULL;")
    database.execSQL("INSERT INTO user ( id_user, external_id ) " +
                            " SELECT               id_user, external_id" +  
                            " FROM                 user_old;")
    
    database.execSQL("CREATE UNIQUE INDEX idx_unique_user ON user (external_id);")
    database.execSQL("PRAGMA foreign_keys=on;")
    database.execSQL("DROP TABLE user_old;")
    //database.endTransaction() 
    }
    
    0 讨论(0)
  • 2020-12-04 12:29
    @Database(entities = {Tablename1.class, Tablename2.class}, version = 3, exportSchema = false)
    

    Change the version number in your RoomDatabase class. Increment the version number.

    0 讨论(0)
  • 2020-12-04 12:30

    Aniruddh Parihar 's answer gave me a hint and it solved.

    Search for a class where you have extended RoomDatabase. There you will find version like below :

    @Database(entities = {YourEntity.class}, version = 1)
    

    just increase the version and problem is solved.

    0 讨论(0)
  • 2020-12-04 12:32

    In my case android:allowBackup="false" making it from true to false worked, as this has given me nightmares before as well, this is the weirdest thing why is this setting enabled by default!

    0 讨论(0)
  • 2020-12-04 12:32

    In order to fix the problem in kotlin:

    First

    @Database(entities = [Contact::class], version = 2)
    

    Second

    val MIGRATION_1_2 = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE Contact ADD COLUMN seller_id TEXT NOT NULL DEFAULT ''")
            }
        }
    

    Third

    private fun buildDatabase(context: Context) = Room.databaseBuilder(
                context.applicationContext,
                EpayDatabase::class.java,
                "epay"
            )
                .addMigrations(MIGRATION_1_2)
                .build()
    

    For more details, check the official documentation

    0 讨论(0)
  • 2020-12-04 12:33

    I got the same error during the training program of Codelabs. Where In one training session I created a project and it ran successfully with all database operations. In the next session, I was working with a different repo, but it was an extension of the previous project, From the first build of the extended app only I had got the error.

    Maybe Studio keeps authentication techniques with room database that's missing with the new build.

    0 讨论(0)
提交回复
热议问题