Relations

What data is linked to what other data. Allows you to assign authors to articles, products to sales, and whatever other structures you can think of.

List Relations

List all relations that exist in Directus. The data returned in this endpoint will be filtered based on the user's permissions. For example, relations that apply to a collection that the current user doesn't have access to are stripped out.

Query Parameters

fields
array

Control what fields are being returned in the object.

limit
integer

A limit on the number of objects that are returned.

offset
integer

How many items to skip when fetching data.

meta
string

What metadata to return in the response.

sort
array

How to sort the returned items. sort is a CSV of fields used to sort the fetched items. Sorting defaults to ascending (ASC) order but a minus sign (-) can be used to reverse this to descending (DESC) order. Fields are prioritized by their order in the CSV. You can also use a ? to sort randomly.

filter

Select items in collection by given conditions.

search
string

Filter by items that contain the given search query in one of their fields.

page
integer

Cursor for use in pagination. Often used in combination with limit.

200 Response

Successful request

401 Response

Error: Unauthorized request

error
object

404 Response

Error: Not found.

error
object
GET /relations
Response Example
{
  "data": [
    {
      "collection": "directus_collections",
      "field": "group",
      "related_collection": "directus_collections",
      "schema": {
        "table": "directus_collections",
        "column": "group",
        "foreign_key_table": "directus_collections",
        "foreign_key_column": "collection",
        "on_update": "NO ACTION",
        "on_delete": "NO ACTION",
        "constraint_name": null
      },
      "meta": {
        "many_collection": "directus_collections",
        "many_field": "group",
        "one_collection": "directus_collections",
        "one_field": null,
        "one_allowed_collections": null,
        "one_collection_field": null,
        "one_deselect_action": "nullify",
        "junction_field": null,
        "sort_field": null,
        "system": true
      }
    }
  ]
}

Create a Relation

Create a new relation.

Query Parameters

fields
array

Control what fields are being returned in the object.

meta
string

What metadata to return in the response.

Request Body

collection_many
string

Collection that has the field that holds the foreign key.

collection_one
string

Collection on the one side of the relationship.

field_many
string

Foreign key. Field that holds the primary key of the related collection.

field_one
string

Alias column that serves as the one side of the relationship.

junction_field
string

Field on the junction table that holds the primary key of the related collection.

200 Response

Successful request

data
object

401 Response

Error: Unauthorized request

error
object

404 Response

Error: Not found.

error
object
POST /relations
Response Example
{
  "data": {
    "id": 1,
    "many_collection": "directus_activity",
    "many_field": "user",
    "one_collection": "directus_users",
    "one_field": null,
    "one_allowed_collections": [],
    "junction_field": null
  }
}

List relations in collection

List all relations that exist in a given collection.

Query Parameters

collection
string

Unique identifier of the collection the relations reside in.

200 Response

Successful request

data
array

401 Response

Error: Unauthorized request

error
object

404 Response

Error: Not found.

error
object
POST /relations‎/{collection}
Response Example
{
  "data": [
    {
      "id": 1,
      "many_collection": "directus_activity",
      "many_field": "user",
      "one_collection": "directus_users",
      "one_field": null,
      "one_allowed_collections": [],
      "junction_field": null
    }
  ]
}

Retrieve a Relation

Retrieve a single relation by unique identifier.

Query Parameters

id
integer

Identifier for the object.

fields
array

Control what fields are being returned in the object.

meta
string

What metadata to return in the response.

200 Response

Successful request

data
object

401 Response

Error: Unauthorized request

error
object

404 Response

Error: Not found.

error
object
GET /relations/{id}
Response Example
{
  "data": {
    "id": 1,
    "many_collection": "directus_activity",
    "many_field": "user",
    "one_collection": "directus_users",
    "one_field": null,
    "one_allowed_collections": [],
    "junction_field": null
  }
}

Delete a Relation

Delete an existing relation.

Query Parameters

id
integer

Identifier for the object.

204 Response

The resource was deleted successfully.

401 Response

Error: Unauthorized request

error
object

404 Response

Error: Not found.

error
object
DELETE /relations/{id}

Update a Relation

Update an existing relation.

Query Parameters

id
integer

Identifier for the object.

fields
array

Control what fields are being returned in the object.

meta
string

What metadata to return in the response.

Request Body

collection_many
string

Collection that has the field that holds the foreign key.

collection_one
string

Collection on the one side of the relationship.

field_many
string

Foreign key. Field that holds the primary key of the related collection.

field_one
string

Alias column that serves as the one side of the relationship.

junction_field
string

Field on the junction table that holds the primary key of the related collection.

200 Response

Successful request

data
object

401 Response

Error: Unauthorized request

error
object

404 Response

Error: Not found.

error
object
PATCH /relations/{id}
Response Example
{
  "data": {
    "id": 1,
    "many_collection": "directus_activity",
    "many_field": "user",
    "one_collection": "directus_users",
    "one_field": null,
    "one_allowed_collections": [],
    "junction_field": null
  }
}