8

I am using Room 2.4.0-alpha01

but it show me this error

Schemas required for migration are not found at path: \app\schemascom.pigeon.mangaer.AppDB/2.json. Cannot generate auto migrations.

here is my code:

@Database(
    exportSchema = true,
    version = 3,
    entities = [Pigeon::class,PairEntity::class],
    autoMigrations = [
        AutoMigration(from = 2,to = 3)
                     ]
   )
abstract class AppDB:RoomDatabase() {
    abstract  fun  pigeonDao():PigeonDao
    abstract fun  pairDao():PairDao
}
Masum
  • 85
  • 3
  • 8

4 Answers4

5

In my case I changed the database version after making the changes and compiling. This modified the generated schema for the old version (2.json), so the new version would still compare to the wrong old version.

Spent some time until I noticed this, so rolling back the changes on the old schema fixed it. This is assuming you use some sort of version control. Without it, you should first build the app without the changes made in the data model. Then bump up the database version and apply the changes (adding/removing columns, etc).

Sergi Mascaró
  • 452
  • 9
  • 13
4

In case this helps anyone. The accepted answer is correct. However, you should add

exportSchema = true

then rebuild your app, and then add the migration block:

autoMigrations = [
    AutoMigration(from = 2,to = 3)
]

This way the compiler will generate the schema for the old version, which it needs for the migration. If you change exportSchema to true at the same time, the compiler will complain that the schema json file is missing. Took me a while to figure that one out.

jilbot
  • 93
  • 8
  • I think this is important to keep in mind. If you change both at the same time, you will get this error. that was what happened to me – Tyler S. Loeper Dec 12 '22 at 22:32
  • 1
    In the case of having a production deployment of an app using “version 2” with exportSchema = false and a new version of an app using version 3 with exportSchema = true, will the automigration Just Work? Or will the migration break because existing deployments don’t have a schema? Hope this makes sense. – Bink Feb 10 '23 at 20:36
3

Room Auto-migration requires you to export your database schema, so it can know how your database was in the previous version, in order to generate the auto-migration. This was stated at a post on Medium by Florina Muntenescu:

⚠️Note: Under the hood, Room auto-migrations rely on the generated database schema, so make sure that the exportSchema option in @Database is true while using autoMigrations. Otherwise it leads to an error: Cannot create auto-migrations when export schema is OFF.

This answer shows how to add the option exportSchema correctly to your project.

cd1
  • 15,908
  • 12
  • 46
  • 47
  • reason of this error is i set exportSchema = false for version 2 – Masum Apr 24 '21 at 05:11
  • 1
    exactly, it's missing the schema of version 2. if you haven't commited the changes for version 3 yet, you can still add that flag to version 2, run your app and generate the schema for version 2, and then add your changes to version 3. if that's not possible, auto migration will only work from version 3 to 4. – cd1 Apr 24 '21 at 15:44
2

My mistake was I remembered to add the Auto migration, but forgot to change the version. I added the migration:

AutoMigration(from = 9, to = 10),

but forgot to change the version:

version = 9

So changing the version to 10:

version = 10

fixed the problem.

Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69