Skip to main content

Command Palette

Search for a command to run...

Directus Basics Part 2 — Understanding Relationships (M2O, O2M, M2M)

In part two of our Directus Basics series, we're breaking down how relationships work between collections — one of the most powerful (and initially confusing) features in Directus.

Updated
4 min readView as Markdown
Directus Basics Part 2 — Understanding Relationships (M2O, O2M, M2M)
W
I'm a full stack developer interested in anything, TanStack Start, Directus, Tailwindcss v4, Shadcn, Zustand and Coolify.

https://www.youtube.com/watch?v=7SNrBzaxreg&t=10s

This is part two of our Directus Basics series. In part one, we set up a Directus instance and created our first collection. Today we're covering one of the most powerful features Directus offers: relationships between collections.

By the end of this article, you'll understand three core relationship types — Many to One (M2O), One to Many (O2M), and Many to Many (M2M). There's a fourth type, Many to Any (M2A), which we'll cover in a future post.

Setting Up the Branding Collection

Continuing from our existing Directus instance, let's create a new collection called branding. Select all the additional default fields Directus offers.

Add two fields:

Field Type
name String (basic input)
description Text (textarea)

Once the collection is set up, add a few entries — I'd recommend at least two different brands so you can see the relationship in action once it's wired up.

Many to One (M2O): Products → Branding

Head over to your products collection. We want to connect a product to a brand.

Logically: many products can belong to one brand. That's a Many to One relationship — many products, to one brand.

Steps:

  1. Create Field → Relational category → Many to One

  2. Key: brand_id

  3. Related Collection: branding

  4. De-select Enable Create Button — you always want to choose a brand from an existing list, not create one on the fly

  5. Save

One to Many (O2M): Branding → Products

Since one brand can have many products, we need to set up the reverse relationship explicitly on the branding collection.

Steps:

  1. Create Field → Relational category → One to Many

  2. Key: products

  3. Related Collection: products

  4. Foreign Key: brand_id — the field we just created on the products side

  5. De-select Enable Create Button

  6. Save

Seeing the Relationship in Action

Go back into your products collection and assign a brand to each product. Make sure at least two products share the same brand so the relationship is visible.

You'll notice each product can only belong to one brand — that's the M2O relationship at work.

Now open the branding collection and click into one of your brands. You'll see the products you just assigned show up automatically — you didn't have to add them manually. Directus handled that because of the relationship you configured.

Many to Many (M2M): Products ↔ Tags

M2M relationships work differently — both sides can have many related records.

A common real-world example: tags on products, used to improve SEO. A single product can have multiple tags, and a single tag can apply to many products.

Create the Tags Collection

Create a new collection called tags, with the same two fields as before:

Field Type
name String
description Text (textarea)

Add three or four tags before moving on.

Wire Up the M2M Relationship

Back in the products collection:

  1. Create Field → Relational category → Many to Many

  2. Key: tags

  3. Related Collection: tags

  4. De-select Enable Create Button

  5. Do not save yet — scroll down and click "Continue in Advanced Field Creation Mode"

  6. In the sidebar, click Relationship, then find Corresponding Field

  7. Check "Create Field" — under Field Name, you should see products appear

  8. Save

Why the advanced flow? Under Corresponding Field, products appears automatically — that's Directus telling you it's about to create the matching relational field back on the tags collection. Unlike the M2O/O2M setup, you don't have to manually configure both sides — Directus builds the reverse relationship for you.

Now, when you add a tag to a product, Directus automatically links that product under the corresponding tag in the tags collection — no manual syncing required.

Summary

Relationship Direction Example
M2O Many records → one related record Products → Branding
O2M One record → many related records Branding → Products
M2M Many ↔ many, both directions Products ↔ Tags

Relationships are one of those Directus features that make a lot more sense once you've built one yourself than they do reading about them — so I'd recommend replicating this setup in your own instance before moving on.

That wraps up part two. In part three, we'll cover Directus permissions — how to control exactly who can see and edit your data.


Found this useful? Follow for more Directus and TanStack Start tutorials, or check out the video version on YouTube.