Modify an Object Schema - Android SDK¶
On this page
The following examples demonstrate how to add, delete, and modify properties in a schema. First, make the required schema change. Then, create a corresponding migration function to move data from the original schema to the updated schema.
The following page demonstrates how to modify schema properties of a local realm. Learn how to modify schema properties of a synced realm.
Assume that each schema change in this example occurs after an application has used each version for some amount of time. New schema version numbers only apply once you open the realm with an updated schema and explicitly specify the new version number, so in order to get to version 3, you would first need to open the app with versions 1 and 2.
A realm using schema version
0 has a
Person object type:
Add a Property¶
The following example adds a
lastName property to the
original Person schema:
Delete a Property¶
The following example uses a combined
fullName property instead of the separate
lastName property in the original Person schema:
Modify a Property Type or Rename a Property¶
The following example modifies the
age property in the
original Person schema by
renaming it to
birthday and changing the type to
To migrate the realm to conform to the updated
Person schema, set the realm's
schema version to
and define a migration function to set the value of
fullName based on the existing
lastName properties and the value of
birthday based on
You can use the RealmConfiguration.shouldDeleteRealmIfMigrationNeeded() builder method when constructing a realm to delete the realm instead of performing a migration when a migration is required. This can come in handy during development when schemas often change.