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.

By | 2024년 11월 29일
Table of Contents

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.

스키마 변경을 위해 DROP TABLE 과 CREATE TABLE 을 막바로 해버리면 위와 같은 오류가 발생한다.

버전 넘버를 올려도 같은 오류가 발생한다.

적절한 스키마 변경 방법은 아래와 같다.

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {
        // 기존 테이블 삭제
        database.execSQL("DROP TABLE IF EXISTS your_table");
        // 새 테이블 생성
        database.execSQL("CREATE TABLE your_table (...)")
    }
};
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
        AppDatabase.class, "database-name")
        .addMigrations(MIGRATION_1_2)
        // 또는
        .fallbackToDestructiveMigration() // 데이터를 보존할 필요가 없다면
        .build();
@Database(entities = {YourEntity.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase {
    public abstract YourDao yourDao();
}

addMigrations() 메소드를 통해서 스키마를 변경해 주어야 한다.

답글 남기기