Realm Schema Migrations on Android: Version Management and Data Safety
Realm schema migrations on Android require a version increment and migration block to prevent crashes when updating data models for existing users.
01 Oct 2025, 03:33 UTC

The Problem: Schema Changes Break Existing Installations
When you modify a Realm data model—adding a field, renaming a property, or changing a type—the local database file on a user's device no longer matches the code. If you release the change without incrementing schemaVersion and supplying a migration, Realm throws a migration exception on the next launch. Existing users cannot open the app until they clear app data or reinstall. The useful takeaway: treat every model change as a migration event, increment the schema version, and verify the upgrade path on a device that already has an earlier version installed.
How Realm Tracks Schema Versions
Realm writes a schema version number into the local database file. When your app opens a Realm, the SDK compares that stored version with the version in RealmConfiguration. If the file version is lower, Realm invokes your migration block to transform the schema and existing objects. If the file version is higher than the configuration version, Realm refuses to open the file. This means migrations only run forward, never backward.
Configuration Example
This Kotlin example assumes Realm Java/Kotlin interop with a schema version bump from 1 to 2. It adds a nullable dueDate field to a Task object.
// Data model
@RealmClass
open class Task : RealmObject() {
var title: String = ""
var completed: Boolean = false
var dueDate: java.util.Date? = null
}
// Realm configuration
val config = RealmConfiguration.Builder()
.schemaVersion(2)
.migration { realm, oldVersion, newVersion ->
if (oldVersion == 1L) {
val taskSchema = realm.schema.get("Task") ?: return@migration
taskSchema.addField("dueDate", java.util.Date::class.java)
}
}
.build()
val realm = Realm.getInstance(config)
Place the configuration where your app initializes Realm, such as an Application subclass or a DI module. The migration lambda runs only when a device upgrades from version 1. Because dueDate is nullable, existing Task objects receive null automatically; no row is lost.
Backfilling Non-Nullable Fields
If the new field cannot be null, assign a value to every existing object inside the migration:
.migration { realm, oldVersion, newVersion ->
if (oldVersion == 1L) {
val taskSchema = realm.schema.get("Task") ?: return@migration
taskSchema.addField("dueDate", java.util.Date::class.java, FieldAttribute.REQUIRED)
realm.executeWrite {
val tasks = it.where("Task").findAll()
for (i in 0 until tasks.size) {
tasks[i].set("dueDate", java.util.Date())
}
}
}
}
Verify the exact method names for your SDK version; Realm Java and the Kotlin SDK expose similar migration concepts but different class names.
Limits and Common Mistakes
- Never skip intermediate versions. If production devices run version 1 and you release version 3, handle 1→2 and 2→3 changes, or write logic that covers oldVersion == 1L with all intermediate field additions.
- Renaming a field deletes data unless you copy it. Realm treats a rename as removal plus addition. Use the migration to read the old value and write it to the new field before removing the old one.
- Schema edits outside a migration block fail. Modify realm.schema only inside the migration callback; attempting it at runtime throws an exception.
- Open Realm instances block migrations. Close every Realm instance in onDestroy or equivalent before the app attempts to open a migrated file.
- Realm Studio is for inspection, not production repair. Use it to verify migrated files locally, not to edit user databases directly.
Verification Checklist
- Install version 1, create several Task objects, then install version 2 over it and confirm the app opens without a migration exception.
- Open the upgraded database in Realm Studio and confirm existing rows retain title and completed values.
- Test a clean install of version 2 to ensure the schema initializes without running legacy migration logic.
- Repeat the upgrade test with a non-nullable field and confirm every existing row has a backfilled value.
Key Takeaway
Realm schema migrations require an incremented schemaVersion and a migration block that transforms existing objects for every prior production version. Test upgrades on real devices before release; missing migrations crash the app for existing users.