Back to Directus

Relationships

content/guides/01.data-model/4.relationships.md

latest3.9 KB
Original Source

Directus supports all standard relationship types, as well as a few more of its own compound types, meant to streamline certain common configurations.

Many to One (M2O)

In a M2O relationship, multiple items from the one collection are linked to one item in a different collection. One field is added to the 'many' collection referencing the primary key of the 'one' collection.

In :product-link{product="editor"}, having a M2O field does not automatically provide navigation to the related collection's items. To achieve this, the related collection requires a One to Many field to be set up.

::callout{icon="material-symbols:info-outline"} M2O Examples

  • Given cities and countries collections, many cities would be assigned to one country.
  • Given orders and customers, many orders would be assigned to one customer.
  • Given books and publishers, many books would be assigned to one publisher.

::

One to Many (O2M)

In a O2M relationship, one item from a collection is linked to multiple items in a different collection.

Creating a O2M interface on the 'many' side of a M2O relationship creates an Alias field, which lets us access related items. This does not create a new database column as the O2M field is purely virtual. It creates an interface within the :product-link{product="editor"} to access items from an O2M perspective.

Many to Many (M2M)

In a M2M relationship, an additional collection is created known as the junction collection. The junction collection stores the primary keys from two related collections, allowing for any number of items to be related between two collections.

You can also have a self-referencing M2M relationship that connects items in the same collection.

::callout{icon="material-symbols:info-outline"} Self-Referencing M2M Examples

  • Given an articles collection, you could configure related articles.
  • Given a users collection, you could configure a friends list.
  • Given papers, you could configure citations.

::

One example is "Related Articles", where each article relates to many other articles.

Many to Any (M2A)

:video-embed{video-id="f711e94f-14c1-48dd-b00c-70a340351412"}

In a M2A relationship, one collection can be related to any item in any collection. This is sometimes known as a matrix field or replicator.

When you configure a M2A in Directus, a M2A Alias field is created as well as a junction collection. The junction collection in a M2A relationship also stores the collection name for related collections.

::callout{icon="material-symbols:school-outline" color="secondary" to="/tutorials/getting-started/create-reusable-blocks-with-many-to-any-relationships"} Read our tutorial on using a Builder (M2A) to create reusable page components. ::

Translations

When you create a Translations interface in Directus, a translations O2M Alias field is created, as well as a languages collection and a junction collection between your main collection and languages. All translated text is stored in the junction collection.

:video-embed{video-id="2d192e76-378b-4540-9d41-2506460a50af"}