Navigation

Modify an Object Schema - Android SDK

On this page

  • Add a Property
  • Delete a Property
  • Modify a Property Type or Rename a Property
  • Migration Functions

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.

Note
Modify Schema Properties of a Synced Realm

The following page demonstrates how to modify schema properties of a local realm. Learn how to modify schema properties of a synced realm.

Note
Versions Update on Realm Open

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:

The following example adds a lastName property to the original Person schema:

The following example uses a combined fullName property instead of the separate firstName and lastName property in the original Person schema:

The following example modifies the age property in the original Person schema by renaming it to birthday and changing the type to Date:

To migrate the realm to conform to the updated Person schema, set the realm's schema version to 3 and define a migration function to set the value of fullName based on the existing firstName and lastName properties and the value of birthday based on age:

Tip
Tip

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.

Give Feedback

On this page

  • Add a Property
  • Delete a Property
  • Modify a Property Type or Rename a Property
  • Migration Functions