1

Issue: Whenever I make changes to the database or the model, I get the following Room data integrity error:

enter image description here

My understanding is that I shouldn't need to increase the version number since I am using .fallbackToDestructiveMigration().

Background:

  • I using DB Browser for SQLite (v3.12.0) to make changes to the database.
  • I frequently make changes to my app/database, which is still in development. So, I am using a .fallbackToDestructiveMigration() (see codelab example).

File: RoomDB.java

@Database(entities =
    {Note.class, Label.class, Join_ScheduleLabel.class, Schedule.class},
    version = 1)
@TypeConverters(DataConverters.class)
public abstract class RoomDB extends androidx.room.RoomDatabase {

    public static final String DATABASE_NAME = "vk_prepop.sqlite";

    private static RoomDB INSTANCE;

    public static RoomDB getInstance(final Context context) {
        if (INSTANCE == null) {
            synchronized (RoomDB.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(
                        context.getApplicationContext(),
                        RoomDB.class,
                        DATABASE_NAME)
                        // Source: https://developer.android.com/training/data-storage/room/prepopulate
                        .createFromAsset(DATABASE_NAME)
                        // Todo: Remove Destructive Migration
                        // Wipes and rebuilds instead of migrating if no Migration object.
                        .fallbackToDestructiveMigration()
                        .build();
                }
            }
        }
        return INSTANCE;
    } 
    public abstract RoomDao getRoomDao();
}

Troubleshooting Steps Taken:

  1. Verifying the entities of the RoomDB.java file match the models and database.
  2. Going into the App Info and tapping "Clear data" (see SO answer).
  3. Uninstalling the app.
  4. Making setting android:allowBackup="false" in the manifest (see SO answer).

Possible Solution: In live-love's answer he says there may be an identityHash mismatch, but I am not sure how to resolve this using DB Browser for SQLite.

yerty
  • 323
  • 7
  • 13

2 Answers2

1

live-love's answer states that there may be an identityHash mismatch. Indeed this was the case. Here is how I resolved the issue using DB Browser for SQLite (v3.12.0).

Step 1: In Android Studio's project panel choose the "Project" view:

enter image description here

Step 2: Then double-click on the json file for your schema:

enter image description here

Step 3: Copy the identityHash in this json file:

enter image description here

Step 4: Open your database in DB Browser for SQLite. Click the "Browse Data" tab. Then from the drop-down menu choose "room_master_table".

enter image description here

Step 5: Compare the identifyHash from the json file in Android Studio to the identityHash in DB Browser for SQLite. If the hashes are different, this can be the cause of you Room data integrity error.

Step 6: So, paste the identityHash from the Android Studio's json file into the identityHash cell in the DB Browser for SQLite.

Step 7: Then press Ctrl+Shift+S to save the database.

Step 8: Click "Close Database".

Step 9: In your app on your phone or emulator go to "App Info" -> "Storage" -> "Clear data".

Step 10: Then in Android Studio press "Run app".

Problem solved... at least for me. If these steps did not help, please review these additional troubleshooting steps.

yerty
  • 323
  • 7
  • 13
1

My understanding is that I shouldn't need to increase the version number since I am using .fallbackToDestructiveMigration().

The fallBackToDestructiveMigration only runs if a migration is required and there is no Migration covering the migration.

In your situation the issue is that you have included the room_master_table in the pre-packaged database and hence the identity_hash columns is available for comparison (which would be incorrect if changes were made to the schema that affected how room generates the identity_hash from the schema).

By including the room_master_table you are introducing an unnecessary complexity.

If you omit this table from the pre-packaged database, then it will be created and populated, with the appropriate identity_hash when it is created from the asset (i.e. when the database doesn't exist). As such you then only have to make the appropriate changes to the asset (the pre-packaged database), delete the current database (e.g. uninstall the App or clear the Apps data) and then run the App.

MikeT
  • 51,415
  • 16
  • 49
  • 68