Connecting Data in Directus: Many-to-Many Relationships

Setting up M2M relationships correctly in Directus, especially configuring the right permissions for your user roles, is key. Let's walk through the process using a common example: connecting Properties
and Amenities
.
Step 1: Creating the M2M Field in Directus
Imagine you have two collections in your Directus project: Properties
and Amenities
. You want to link them so a property can list multiple amenities (like "pool," "gym," "parking"), and an amenity can be associated with multiple properties.
Here’s how to set up the M2M field, starting from the Properties
collection's data model:
Navigate to Settings: Go to
Settings
>Data Model
and select yourProperties
collection.- Create a New Field: Click "Create Field in advanced mode" and Choose the "Many-to-Many (M2M)" field type.
Configure the Relationship:
Key: Give your field a unique identifier (e.g.,
amenities
). This will be the field name within theProperties
collection.Related Collection: Select the collection you want to link to – in this case,
Amenities
.Corresponding Field: This is where Directus sets up the other side of the M2M relationship.
Click "Create New Field". Directus intelligently suggests creating the necessary relational field on the
Amenities
collection.Give this corresponding field a Key (e.g.,
properties
). This field will appear onAmenities
items, showing which properties they are linked to.Directus will automatically handle the creation of the underlying "junction" collection (usually named
properties_amenities
) that stores these links.
Save the Field: Configure any interface options as needed and save the new
amenities
field.
You've now established the two-way M2M relationship within your Directus data model!
Step 2: Configuring Role Permissions (Crucial!)
Creating the relationship is only half the battle. You need to define which roles can manage these connections via Settings
> Roles & Permissions
. Let's focus on an "Editor" role and explore two permission setups:
Scenario 1: Editors Can Fully Manage Amenities (Create, Link, Unlink)
If you want Editors to link existing amenities and create new amenities directly from the Properties
item form:
Amenities
Collection Permissions: The Editor role needs Create, Read, Update, Delete (or appropriate subset) permissions on theAmenities
collection itself. This allows them to view, select, and potentially add/edit amenities.properties_amenities
(Junction Collection) Permissions: The Editor role also needs Create, Read, Update, Delete permissions on the automatically generatedproperties_amenities
junction collection. Linking/unlinking amenities involves creating/deleting rows in this junction collection.
Scenario 2: Editors Can Only Link/Unlink Existing Amenities
If Editors should only select from a list of existing amenities (they cannot create or modify amenities):
Amenities
Collection Permissions: The Editor role only needs Read permission on theAmenities
collection. This allows them to see and select from the list.properties_amenities
(Junction Collection) Permissions: The Editor role still needs Create, Read, Update, Delete permissions on theproperties_amenities
junction collection. They must be able to create (link) and delete (unlink) the relationship records in the junction collection.