Directus icon

Directus

Consume Directus API

Overview

This node operation updates a relation in the Directus system. Relations in Directus define how collections (tables) relate to each other, typically through foreign keys or join tables. The "Update Relation" operation allows users to modify an existing relation's properties by specifying the parent collection and the field that holds the related primary key.

Common scenarios where this node is beneficial include:

  • Adjusting the metadata or configuration of an existing relation between two collections.
  • Changing how two collections are linked without recreating the entire relation.
  • Updating relation-specific settings such as cardinality, junction tables, or meta information.

Practical example:

  • Suppose you have a books collection with a relation field author linking to an authors collection. You want to update the relation to add or change metadata like which field represents the "one" side in a one-to-many relationship. This node lets you patch that relation by providing the collection name (books), the relational field (author), and the updated relation data in JSON format.

Properties

Name Meaning
Collection Unique name of the parent collection where the relation exists. Example: books.
Field Name of the field in the collection that holds the related primary key. Matches the DB column.
Data (JSON) Partial relation object in JSON format describing the updates to apply to the relation.

The Data (JSON) property expects a partial relation object as defined by Directus API. For example, it can include metadata fields like "meta": { "one_field": "articles" }.

Output

The output is a JSON object representing the updated relation as returned by the Directus API. It contains all the properties of the relation after the update, including any changes made.

Example output structure (simplified):

{
  "id": "relation-id",
  "collection": "books",
  "field": "author",
  "related_collection": "authors",
  "meta": {
    "one_field": "articles"
  },
  ...
}

No binary data output is involved in this operation.

Dependencies

  • Requires a valid connection to a Directus instance via an API key credential configured in n8n.
  • The node uses the Directus REST API endpoint for relations: PATCH /relations/{collection}/{field}.
  • Proper permissions on the Directus API to update relations are necessary.

Troubleshooting

  • Invalid Collection or Field: If the specified collection or field does not exist, the API will return an error. Verify the collection and field names are correct and exist in your Directus instance.
  • Malformed JSON in Data: The Data (JSON) input must be valid JSON. Invalid JSON syntax will cause parsing errors. Use the built-in editor or validate JSON before running.
  • Insufficient Permissions: Ensure the API key used has permission to update relations; otherwise, the request will fail with authorization errors.
  • API Connectivity Issues: Network problems or incorrect API URL/credentials will cause failures. Check connectivity and credentials configuration.
  • Partial Updates: Only provide the fields you want to update in the JSON data. Overwriting the entire relation object unintentionally may cause issues.

Links and References

Discussion