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
- Each article has one author
- Each author can have many articles
- The
authorfield inarticlesstores 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
- The
articlesfield onauthorsis virtual (alias field) - No database column exists for
articleson the authors table - Querying
authors.articlesreturns all articles by that author
Usage in Queries
Many-to-Many (M2M)
Connects items from two collections through a junction (pivot) collection.Example: Articles ↔ Tags
- articles - Original collection
- tags - Related collection
- articles_tags - Junction collection
Usage in Items
Query with M2M
Many-to-Any (M2A)
Polymorphic relationships that can relate to items in multiple different collections.Example: Comments on Multiple Collections
comments collection has:
item- Stores the related item IDcollection- Stores the collection name
Usage
Creating Relationships
Via API
~/workspace/source/api/src/services/relations.ts:191-200), creating a relationship:
- Validates required fields (
collection,field) - Creates/updates the foreign key constraint in the database
- Saves relationship metadata to
directus_relations - 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:sort field to the junction collection:
Deleting Relationships
~/workspace/source/api/src/services/fields.ts:722-768), deleting a relationship:
- Removes the virtual O2M field if it exists
- Drops the foreign key constraint from the database
- Deletes the relationship metadata
- May also delete related M2O field depending on configuration
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
Related Concepts
- Collections - Tables that contain related items
- Fields - Foreign key fields that store relationships
- Items - Records that are linked through relationships