Connect

Relational Data

Directus enables you to manage and interact with relational data. This section will guide you through the different types of relationships and how to work with them.

By default, Directus only retrieves the reference value of a relational field in your items. To also retrieve nested data of a relational field the fields parameter in REST can be used, or regular nested queries in GraphQL. This allows you to retrieve the author of your article included in the articles data, or fetch related log entry points for your app's analytics data for example.

Creating / Updating / Deleting

Similarly to fetching, relational content can be modified deeply as well.

Many-to-One

Many-to-One relationships are fairly straightforward to manage relationally. You can submit the changes you want as an object under the relational key in your collection. For example, if you wanted to create a new featured article on your page, you could submit:

{
  "featured_article": {
    "title": "This is my new article!"
  }
}

This will create a new record in the related collection, and save its primary key in the featured_article field for this item. To update an existing item, provide the primary key with the updates, and Directus will treat it as an update instead of a creation:

{
  "featured_article": {
    "id": 15,
    "title": "This is an updated title for my article!"
  }
}

Seeing that the Many-to-One relationship stores the foreign key on the field itself, removing the item can be done by nullifying the field:

{
  "featured_article": null
}

One-to-Many and Many-to-Many

One-to-Many, and therefore Many-to-Many and Many-to-Any, relationships can be updated in one of two ways:

Basic

The API will return one-to-many fields as an array of nested keys or items (based on the fields parameter). You can use this same structure to select what the related items are:

Examples

Countries that have a list of cities.

{
  "cities": [2, 7, 149]
}

You can also provide an object instead of a primary key in order to create new items nested on the fly, or an object with a primary key included to update an existing item:

Examples
{
  "cities": [
    2, // assign existing city with id of 2 to be a child of the current item
    {
      "name": "A new nested item" // create a new city
    },
    {
      "id": 149,
      "name": "changed city name" // assign and update existing city with id of 149
    }
  ]
}

To remove items from this relationship, omit them from the array:

{
  "cities": [2, 149]
}

This method of updating a one-to-many is very useful for smaller relational datasets.

"Detailed"

Alternatively, you can provide an object detailing the changes as follows:

{
  "cities": {
    "create": [{ "name": "A new city" }],
    "update": [{ "id": 149, "name": "Update an existing city" }],
    "delete": [7]
  }
}

This is useful if you need to have more tightly control on staged changes, or when you're working with a big relational dataset.

Many-to-Any (Union Types)

Many-to-Any fields work very similar to a "regular" many-to-many, with the exception that the related field can pull in the fields from any of the related collections, for example:

{
  "sections": [
    {
      "collection": "headings",
      "item": {
        /* headings fields */
      }
    },
    {
      "collection": "paragraphs",
      "item": {
        /* paragraphs fields */
      }
    }
  ]
}

Get once-a-month release notes & real‑world code tips...no fluff. 🐰