Many to Many relationships between two collections

I have two collections, let’s call them “parent” and “child”. I want to set up a many-to-many relationship between these two collections and have it be available on both collections.

What I’m having issues with is that last bit - being able to see the related data through either collection.

  • If I’m querying the “parent” collection, i would like to be able to include fields from “child” in my GQL.
  • Alternatively, if I’m querying “child”, I would like to be able to include fields from “parent”

What happens, though, is this:

  1. I create a collection called “parent”
  2. I create a collection called “child”
  3. I add a M2M field to “parent” called “children” and link it to the child collection. The “parent” collection now has a field to link records from “child” but no field exists on the “child” collection
    3a. A pivot-table style collection called parent_child has been added to directus that just has id, parent_id, and child_id
  4. I attempt to add a field called “parents” to the “child” collection, thinking it will make use of the parent_child collection, but when I finish creating that field it makes another collection called child_parent with the exact same fields.

How do I set up the child collection so that the M2M relationship defined in “parent” is settable/usable/queryable/visible/etc?

Hey there! Welcome to the community @julisana :waving_hand:

I think it’s just as simple as adding the corresponding field when you’re creating the relationship for this.

Here’s a quick video of what that looks like:

The key is making sure you set up the corresponding field on both sides of the many-to-many relationship when you’re initially creating it. This ensures the relationship works bidirectionally and you can navigate between collections from either direction.