Specify a Primary Key - React Native SDK
On this page
Overview
To specify a property as an object type's primary key, set the schema's
primaryKey
field to the property name.
Note
A primary key is a property that uniquely identifies an object. Realm Database automatically indexes primary key properties, which allows you to efficiently read and modify objects based on their primary key.
If an object type has a primary key, then all objects of that type must include the primary key property with a value that is unique among objects of the same type in a realm. An object type may have at most one primary key. You cannot change the primary key property for an object type after any object of that type is added to a realm and you cannot modify an object's primary key value.
Example
The following Task
object schema specifies the _id
property as its
primary key.
const TaskSchema = { name: 'Task', properties: { _id: 'objectId', _partition: 'string?', name: 'string', status: 'string', }, primaryKey: '_id', };