10

I was trying to implement a simple auto migration that just adds a nullable string field to my only table, but for some reason I am getting the following error in the autoMigrations = [AutoMigration(from = 1, to = 2)] line:

Type mismatch: inferred type is AutoMigration but KClass<*> was expected

I say that this is a weird error, because even the documentation has it this way.

The full code is below:

@Database(
    version = 2,
    entities = [Note::class],
    autoMigrations = [AutoMigration(from = 1, to = 2)]
)
@TypeConverters(Converters::class)
abstract class NotesDB : RoomDatabase() {
    abstract fun noteDao(): NoteDao

    companion object {
        @Volatile
        private var INSTANCE: NotesDB? = null

        fun getDB(context: Context): NotesDB {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    NotesDB::class.java,
                    "notesDB"
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}
ByteSeb
  • 147
  • 6
  • 1
    It is is useful, I am also getting another error in the same line that I think is related to the first one: Database.autoMigrations can only be called from within the same library group (groupId=androidx.room) – ByteSeb Jul 04 '21 at 05:45
  • Considering that I'm getting the same error and this error has led me just to this page, I be leave it might be a new bug or there should be some other requirements that we are not satisfying... – AKTanara Jul 05 '21 at 21:37
  • 1
    @AKTanara I had to use a manual migration instead. – ByteSeb Jul 06 '21 at 20:26

2 Answers2

4

Change room version in your build.gradle to the newest one (2.4.0-alpha04 at the moment). Then if you will get an error about room.schemaLocation, look at this answer https://stackoverflow.com/a/44424908/6568162

0

set up export schema = true.

It is possible you will still get the error, In my case downgrading the room version and again upgrading helped.