Docs Menu

Define a Realm Object Schema - Swift SDK

On this page


Class names are limited to a maximum of 57 UTF-8 characters.


For reference on which types Realm Database supports for use as properties, see Supported Property Types - Swift SDK.


Property names are limited to a maximum of 63 UTF-8 characters.

Primary keys enforce uniqueness among objects in a realm.

You can efficiently find, update, and upsert objects with a primary key.

As long as an object is managed by a realm, that object's primary key value is immutable.

Indexes make queries using equality and IN operators faster in exchange for slightly slower writes. Indexes take up more space in the realm file. It's best to only add indexes when optimizing the read performance for specific situations.

Realm supports indexing for string, integer, boolean, Date, UUID, ObjectId, and AnyRealmValue properties.

New in version 10.8.0: UUID and AnyRealmValue types

Ignored properties behave exactly like normal properties. They can't be used in queries and won't trigger Realm notifications. You can still observe them using KVO.


Realm automatically ignores read-only properties.

See also: Define a Relationship in the App Services UI

If you're using Realm Sync, you can also define your relationships in the App Services UI.

A to-one relationship maps one property to a single instance of another object type. For example, you can model a person having at most one companion dog as a to-one relationship.

To-one relationships must be optional

When you declare a to-one relationship in your object model, it must be an optional property. If you try to make a to-one relationship required, Realm throws an exception at runtime.

A to-many relationship maps one property to zero or more instances of another object type. For example, you can model a person having any number of companion dogs as a to-many relationship.

An inverse relationship property is an automatic backlink relationship. Realm Database automatically updates implicit relationships whenever an object is added or removed in a corresponding to-many list or to-one relationship property. You cannot manually set the value of an inverse relationship property.

An embedded object exists as nested data inside of a single, specific parent object. It inherits the lifecycle of its parent object and cannot exist as an independent Realm object. Realm automatically deletes embedded objects if their parent object is deleted or when overwritten by a new embedded object instance.

Realm Uses Cascading Deletes for Embedded Objects

When you delete a Realm object, any embedded objects referenced by that object are deleted with it. If you want the referenced objects to persist after the deletion of the parent object, your type should not be an embedded object at all. Use a regular Realm object with a to-one relationship instead.

←  Usage Examples - Swift SDKConfigure & Open a Realm - Swift SDK →
Give Feedback
© 2022 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.