Hi
the migration of optional kotlin objects in realm generates the following error.
java.lang.RuntimeException: Unable to create application com.merqueo.app.AppController: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Property 'ProductTracker.departmentId' has been made optional.
- Property 'ProductTracker.shelfId' has been made optional.
- Property 'ProductTracker.price' has been made optional.
- Property 'ProductTracker.specialPrice' has been made optional.
- Property 'ProductTracker.bestPriceGuaranteed' has been made optional.
- Property 'ProductTracker.position' has been made optional.
Don't put ? after the type
@Zhuinden in this example project from realm, class model contain optionals values

@Jparrgam Yes, and that is what the exception says. You made the fields optional, but the on-disk schema still things they are required. So you need to migrate the schema: https://realm.io/docs/java/latest/#migrations
You could set a default valu, like var name : String = ""
Realm's sample contains optional fields because the underlying schema is not @Required (aka nullable) too.
Related question
In migration I'm adding few optional fields:
realm.schema.get("Class")?.apply {
addField("lat", Double::class.java)
addField("lng", Double::class.java)
}
that is how they look in model
var lat: Double? = null
var lng: Double? = null
but after migration I got exception
Migration is required due to the following errors: - Property 'Class.lat' has been made optional.
That's not possible if your migration actually ran, unless you had previously had lat: Double = 0.0 or something. :/
I think a bell rings somewhere. If I remember correctly then in in Kotlin Double:class is actually double in Java, not Double.
So changing nullability in Kotlin migrations require you to set nullable(true) explicitely. We had an older issue at some point, but I cannot find it right now
Ah yeah. Here it is: https://github.com/realm/realm-java/issues/4701#issuecomment-307489143
I don't know why we didn't add this to the Kotlin section of the website. I'll do that now.
@cmelchior Yeah, javaPrimitiveType is double, but this guy has a Double? so "making the field nullable" shouldn't be a problem. I think?
EDIT: oh, apparently you'd have to use javaObjectType. Hmm, I guess I glossed over that, whoops.
EDIT: Apparently I even had a stack overflow answer on this, I just forgot over the past year XD
I added it to the website docs under the Kotlin section. Should be online shortly
@cmelchior Thank you!
Your suggestion helped me
I have rewriten migration to this:
realm.schema.get("Class")?.apply {
addField("lat", Double::class.java).setNullable("lat", true)
addField("lng", Double::class.java).setNullable("lng", true)
}
@Zhuinden
I think the problem was that in model there was nullable field but migration was adding not nullable field to DB
I found that you can also use Double::class.javaObjectType which will give you the wrapper class
Most helpful comment
@cmelchior Thank you!
Your suggestion helped me
I have rewriten migration to this:
@Zhuinden
I think the problem was that in model there was nullable field but migration was adding not nullable field to DB