Realm migration from a required variable to nullable
I had a variable which was a REQUIRED field in my previous version of realm. But for the newer version i want it to be not required but a nullable one. How do i do it through realm migration?
You can check the example in migrationExample provided by Realm team
if (oldVersion == 2) {
   RealmObjectSchema personSchema = schema.get("ClassName");
   personSchema.setNullable("nullableFielName", true);
}
If you have already synced with Realm Object Server, please check this: https://realm.io/docs/java/latest/#syncing-migrations
You can change the table in realm by either doing this First you declare your variable as optional
open class YourModel: RealmObject() {
@SerializedName("english") var english: String? = "" }
and then do this in your migration file
 override fun migrate(realm: DynamicRealm?, oldVersion: Long, newVersion: Long) {
    val schema = realm?.schema
    if (oldVersion < 1L) {
                schema?.get("YourModel")
                        ?.removeField("english")
                        ?.addField("english", String::class.java)}
or
 schema?.get("YourModel")
                   ?.setNullable("english",true)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With