Data Model Updates
How to rename entities and properties, change property types in ObjectBox.
Last updated
Was this helpful?
How to rename entities and properties, change property types in ObjectBox.
Last updated
Was this helpful?
ObjectBox manages its data model (schema) mostly automatically. The data model is defined by the entity classes you define. When you add or remove entities or properties of your entities, ObjectBox takes care of those changes without any further action from you.
For other changes like renaming or changing the type, ObjectBox needs extra information to make things unambiguous. This is done by setting a unique ID (UIDs) as an annotation, as we will see below.
ObjectBox keeps track of entities and properties by assigning them unique IDs (UIDs). All those UIDs are stored in a file objectbox-models/default.json
(Java, Kotlin) or lib/objectbox-model.json
(Dart) which you should add to your version control system (e.g. git). If you are interested, we have . But let’s continue with how to rename entities or properties.
In short: To make UID-related changes, put an @Uid
annotation (Java, Kotlin) or @Entity(uid: 0)/@Property(uid: 0)
(Dart) on the entity or property and build the project to get further instructions. Repeat for each entity or property to change.
So why do we need that UID annotation? If you simply rename an entity class, ObjectBox only sees that the old entity is gone and a new entity is available. This can be interpreted in two ways:
The old entity is removed and a new entity should be added, the old data is discarded. This is the default behavior of ObjectBox.
The entity was renamed, the old data should be re-used.
So to tell ObjectBox to do a rename instead of discarding your old entity and data, you need to make sure it knows that this is the same entity and not a new one. You do that by attaching the internal UID to the entity.
The same is true for properties.
Now let’s walk through how to do that. The process works the same if you want to rename a property:
Step 1: Add an empty UID to the entity/property you want to rename:
Step 2: Build the project (in Dart, run pub run build_runner build
). The build will fail with an error message that gives you the current UID of the entity/property:
Step 3: Apply the UID from the [Rename] section of the error message to your entity/property:
Step 4: The last thing to do is the actual rename on the language level (Java, Kotlin, etc.):
Step 5: Build the project again, it should now succeed. You can now use your renamed entity/property as expected and all existing data will still be there.
Repeat the steps above to rename another entity or property.
Since in Python there isn't a build step, the steps for renaming an Entity are different. Say we want to rename the Entity MyName
to MyNewName
:
Step 1: Find out the UID of the entity MyName
:
Step 2: Rename MyName
to MyNewName
and explicitly specify the old UID:
This makes possible for Objectbox to associate the old entity to the new one, and retain persisted data. If you don't specify the old UID, Objectbox will discard the old data and add a fresh new entity called MyNameName
to the schema.
ObjectBox does not support migrating existing property data to a new type. You will have to take care of this yourself, e.g. by keeping the old property and adding some migration logic.
There are two solutions to changing the type of a property:
Add a new property with a different name (this only works if the property has no @Uid annotation already):
Set a new UID for the property so ObjectBox treats it as a new property. Let’s walk through how to do that:
Step 1: Add the @Uid
annotation to the property where you want to change the type:
Step 2: Build the project. The build will fail with an error message that gives you a newly created UID value:
Step 3: Apply the UID from the [Change/reset] section to your property:
Step 4: Build the project again, it should now succeed. You can now use the property in your entity as if it were a new one.
Repeat the steps above to change the type of another property.