Search code examples
androidkotlinmigrationrealm

Realm Kotlin migration Android String


Can't find an answer. I'm trying to migrate Realm database. Need to create a table DBRequest with 2 fields: Int and String

if (oldVersion == 5L) {
    val s = scheme.create("DBRequest")
    s.addField("apiCode", Int::class.java)
}

And that's ok! But if I'm trying to add String type as well

    s.addField("jsonRequest", String::class.java)

it throws an error:

Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors: - Property 'DBRequest.jsonRequest' has been made required.

I have no idea how to sort it out. I've tried smth like this:

s.addField("jsonRequest", String::class.javaPrimitiveType!!)
-or-
s.addField("jsonRequest", String::class.javaObjectType)

Nothing helps :(


Solution

  • Add this to the migration

    s
        .addField("jsonRequest", String::class.java)
        .setRequired("jsonRequest", true)