3
votes

I have added some new Entities(tables) to existing database. So configured those in database configuration-

@Database(entities = {existingTable1.class, existingTable2.class, existingTable3.class, existingTable4.class,
        newTable1.class, newTable2.class
        }, version = 1, exportSchema = false)

Since, the app is in development stage I used to avoid migration. So I have unistall the app from device and install through Android Studio (Run). But its giving following error-

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.

In my manifest allowBackup is set to false as below-

android:allowBackup="false"

EDITED

I have one thing to mention. I have a sqlite database file in asset folder. I just copy it into corresponding app DB folder on installation. So when new table with lots of pre-populated data need to added, I just update that sqlite database file in asset folder, then adjust relevant entities and build/run the project. This way, it was working fine, but this time it is producing this issue.

3
Did you run atleast once after setting android:allowBackup="false" ?Manohar
make sure to clear data and cacheRavi Rajput
@ManoharReddy, yes. But after installing from the device, why this migration is required :OSadat
@Sadat open app setting , clear data manually and check once .Manohar
@ManoharReddy, which "app setting"? would you please specify?Sadat

3 Answers

2
votes

If you don't need data to save and don't care about them , just want to tell Room you are migrating you can use a .fallbackToDestructiveMigration() like this.

database = Room.databaseBuilder(context.getApplicationContext(),
                        UsersDatabase.class, "Sample.db")
                .fallbackToDestructiveMigration()
                .build();

It will do the "uninstall" for you .

You can refer to this article here

2
votes

If you are prepackaging your database in your APK. Then delete 'room_master_table' from it. You can SQLborwser for it.

0
votes

When you change a Room database structure, you should create a way to update the existing database. If first structure was version 1, the second should be version 2. But Room needs a way to update the existing database to the new structure. You should create a migration function.

static RoomDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (RoomDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        RoomDatabase.class, "your_database")
                        .addMigrations(MIGRATION_1_2)
                        .build();                
            }
        }
    }
    return INSTANCE;
}

private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("CREATE TABLE IF NOT EXISTS new_table...
    }
};

if you only want to start from the beginning without updating your current dabase, then you can delete it to force the database creation.

    static RoomDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (RoomDatabase.class) {
            if (INSTANCE == null) {
                // do not forget to remove this line if it is not needed 
                context.getApplicationContext().deleteDatabase("your_database");
                // Create database here 
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        RoomDatabase.class, "your_database")...
                ...