Search code examples
joincollectionsrelationshipone-to-manydirectus

Combining Collections in Directus


I've setup a Directus CMS project with 3 collections as follows:

  1. Actors
  2. Movies
  3. TV Shows

I'd like to associate Movies and TV Shows with Actors in a multi-select ordered list. I've tried looking at the one-to-many relationship and some others and can't seem to figure out how to build this. Ideally I would like it to look roughly like:

  • Editing Actor record John Smith
  • Multi-select array (or possibly some other interface) that includes all TV Shows and Movies
  • All TV Shows and Movies are shown by in multi-select by field "Title" of which both collections have
  • User can check boxes for individual TV Shows or Movies and order them in the multi-select as they'd like.

Do I need an intermediary junction collection? Am I missing something obvious here?

From initial impressions Directus looks to be an awesome CMS!


Solution

  • you were on the right track, but the relationship type you are looking for is a "many-to-many" (or M2M). This is because a movie can have many actors, and actors can be in many movies.

    You can see the Directus documentation for this relationship setup here:

    https://docs.directus.io/guides/relationships.html#many-to-many

    You will add two junction tables, something like actor_movies and actor_tv_shows, each with a schema like this:

    • id — The primary key of the relationship
    • actor_id — The foreign key that holds the actor id
    • movie_id — The foreign key that holds the movie id

    Therefore, each record in these tables link an actor to a Movie or TV Show.