Docs Menu

Relationships - Swift SDK

On this page

  • Key Concepts
  • To-One Relationship
  • To-Many Relationship
  • Inverse Relationship
  • Summary

Realm Database allows you to define explicit relationships between objects. A relationship is an object property that references another Realm object instead of a primitive data type. Define a relationship by setting an object property's type to another object in the model.

Realm Database doesn't use bridge tables or explicit joins to define relationships as you would in a relational database. Realm handles relationships through embedded objects or reference properties to other Realm objects. You read from and write to these properties directly. This makes querying relationships as performant as querying against any other property.

There are three primary types of relationships between objects:

A to-one relationship means that an object relates to one other object. You define a to-one relationship for an object type in its object schema. Specify a property where the type is the related Realm object type.

Setting a relationship field to null removes the connection between objects. Realm does not delete the referenced object, though, unless it is an embedded object.

Tip

To learn how to define a to-one relationship, see Define a To-One Relationship Property.

A to-many relationship means that an object relates to more than one other object. In Realm Database, a to-many relationship is a list of references to other objects.

Tip

To learn how to define a to-many relationship, see Define a To-Many Relationship Property.

Relationship definitions in Realm Database are unidirectional. An inverse relationship links an object back to an object that refers to it. You must explicitly define a property in the object's model as an inverse relationship. Inverse relationships can link back to objects in a to-one or to-many relationship.

For example, consider a task tracker with the to-many relationship "User has many Tasks". This does not automatically create the inverse relationship "Task belongs to User". To create the inverse relationship, add a User property on the Task that points back to the task's owner. When you specify the inverse relationship from task to user, you can query on that. If you don't specify the inverse relationship, you must run a separate query to look up the user to whom the task is assigned.

Important

You cannot manually set the value of an inverse relationship property. Instead, Realm Database updates implicit relationships when you add or remove an object in the relationship.

Relationships can be many-to-one or many-to-many. So following inverse relationships can result in zero, one, or many objects.

Tip

To learn how to define an inverse relationship, see Define an Inverse Relationship Property.

  • A relationship is when an object property references another object instead of a primitive type.
  • Relationships are direct references. You can access related objects through a relationship property without writing a join.
  • Realm Database supports to-one, to-many, and inverse relationships.
←  Write Transactions - Swift SDKSchema Versions & Migrations - Swift SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

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