Skip to main content

What are Relationships?

Relationships define how data in one collection relates to data in another collection. They enable you to build complex, normalized data models by linking records together, similar to foreign keys and JOINs in SQL databases.
Directus supports all standard relational patterns: Many-to-One (M2O), One-to-Many (O2M), Many-to-Many (M2M), and Many-to-Any (M2A) polymorphic relationships.

Relationship Structure

Each relationship consists of two parts:

Relationship Metadata

Many-to-One (M2O)

The most common relationship type, where many items in one collection relate to a single item in another.

Example: Articles → Authors

In this example:
  • Each article has one author
  • Each author can have many articles
  • The author field in articles stores the author’s ID
  • A foreign key constraint ensures referential integrity

Usage in Items

One-to-Many (O2M)

The inverse of M2O, providing a virtual field to access related items.

Example: Authors → Articles

In this example:
  • The articles field on authors is virtual (alias field)
  • No database column exists for articles on the authors table
  • Querying authors.articles returns all articles by that author

Usage in Queries

Response:

Many-to-Many (M2M)

Connects items from two collections through a junction (pivot) collection.

Example: Articles ↔ Tags

Requires three collections:
  1. articles - Original collection
  2. tags - Related collection
  3. articles_tags - Junction collection
Junction collection structure:
Relationship configuration:

Usage in Items

Query with M2M

Response:

Many-to-Any (M2A)

Polymorphic relationships that can relate to items in multiple different collections.

Example: Comments on Multiple Collections

The comments collection has:
  • item - Stores the related item ID
  • collection - Stores the collection name

Usage

Creating Relationships

Via API

From the source code (~/workspace/source/api/src/services/relations.ts:191-200), creating a relationship:
  1. Validates required fields (collection, field)
  2. Creates/updates the foreign key constraint in the database
  3. Saves relationship metadata to directus_relations
  4. Optionally creates reverse O2M virtual field

Foreign Key Actions

  • CASCADE - Delete/update related items
  • SET NULL - Set foreign key to NULL
  • RESTRICT - Prevent deletion if related items exist
  • NO ACTION - Database-specific default

Querying Relationships

Field Expansion

Deep Nesting

Filtering on Relationships

Aggregation

Sorting in Relationships

For M2M and O2M relationships, enable manual sorting:
Add a sort field to the junction collection:

Deleting Relationships

From the source code (~/workspace/source/api/src/services/fields.ts:722-768), deleting a relationship:
  1. Removes the virtual O2M field if it exists
  2. Drops the foreign key constraint from the database
  3. Deletes the relationship metadata
  4. May also delete related M2O field depending on configuration
Deleting a relationship removes the foreign key constraint and may orphan data. Ensure you handle related data appropriately.

Common Use Cases

Content Authoring

Link articles to authors, categories, and tags with M2O and M2M relationships for rich content organization.

E-commerce

Connect products to categories, brands, and variants; orders to customers and products with complex relationship chains.

User Management

Associate users with roles, teams, and departments through hierarchical and many-to-many relationships.

Media Libraries

Relate images and files to multiple content types using M2A relationships for flexible asset management.

Best Practices

Use Foreign Keys: Always create foreign key constraints for data integrity and performance.
Name Junctions Clearly: Use descriptive names like articles_tags or users_roles for junction collections.
Set Delete Actions: Choose appropriate on_delete actions (CASCADE, SET NULL) based on your data requirements.
Optimize Queries: Use field selection to limit nested data and prevent over-fetching.
Index Foreign Keys: Ensure foreign key fields are indexed for efficient JOIN operations.
  • Collections - Tables that contain related items
  • Fields - Foreign key fields that store relationships
  • Items - Records that are linked through relationships