0

For some reason, I started getting this 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.

I did integrate some changed to the app, but never touched the Room entities.

I exported the database before the changes and after and I noticed that for one of the entities this column

...`created_at` TEXT,...

changed to this

...`created_at` TEXT NOT NULL,...

I never did anything to it. It's not THAT big of a deal, i can do a migration and perhaps solve it, but I'd still want to figure out the root cause of it, and who knows what else might be affected.

What could I have possibly done to make it automatically update the model in that way?

Some things I did in case it helps narrow it down:

  • integrated a KMM module to the existing Android project
  • updated firebase-crashlytics
  • converted project level (not app leve) build.gradle to kotlin, so build.gradle.kts
  • increased gradle-wrapper.properties distributionUrl from 6.6.1 to 7.3.3
  • increased classpath com.android.tools.build:gradle from 4.0.1 to 7.2.2
  • increased kotlin version from 1.6.21 to 1.8.0
  • increased com.google.gms:google-services from 2.2.1 to 2.9.4

In my mind NONE of these things should have affected my database model ... but apparently something did.

Any ideas ?!

EDIT

I just noticed that at some point 2 migrations ago (so not anytime soon), I did this

database.execSQL("ALTER TABLE `GridItem` ADD COLUMN `created_at` TEXT NOT NULL DEFAULT ''");

It seems I did defined it as NOT NULL, but then ... why would it simply say TEXT now, when I exported the database, when I in fact defined it as NOT NULL a while back ?! This is so confusing !

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106

1 Answers1

0

What could I have possibly done to make it automatically update the model in that way?

I exported the database before the changes and after and I noticed that for one of the entities this column

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.

When you compile the project Room creates a hash of the schema according to how Room interprets the @Entity annotated classes. As can be found in the generated java. e.g. enter image description here

When at run time opening/connecting the database and if the database exists the compiled hash is compared with the hash stored in the room_master_table, if the two hashes do not match then Room expects an increase in the version number. If the version has not been increased then you get the Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. .... message and exception.

By exporting the database you are maintaining the hash as of the time of the export.

NOT NULL is part of the schema and thus any change that changes the status will affect the schema and thus the hash.

  • NOT NULL is determined according to Room's set of rules when examining the @Entity annotated class. The rules are quite complex as there are potential factors, such a primary keys (which Room will not allow to be null even though SQLite does allow nulls) that are taken into consideration. Room considers implicit as well as explicit factors.
  • The easiest way to determine what is what is to refer to the generated java and inspect the createAllTabales method in the class that is the same name as the @Database annotated class but suffixed with _Impl (also where the hash can be found).
MikeT
  • 51,415
  • 16
  • 49
  • 68