Search code examples
many-to-manydirectus

Symmetric many-to-many relationship in Directus


I have two collections in Directus (data platform that provides many features including REST API over database records). There is option to set relationship between different collections.

I set many-to-many relationship between collectionA and collectionB. Special system collection (junction table) called collectionA_collectionB was automatically created. Now I can add items from collectionB while editing collectionA items.

But same time I want to add collectionA items while editing collectionB items and I don't find way to add existing junction table. When I add many-to-many relationship between collectionB and collectionA - new junction table is created.

How can I use same junction table to achieve symmetric many-to-many relationship?


Solution

  • From the Directus UI you can delete the relation from CollectionA and also delete the junction table collectionA_collectionB and then create again the M2M to CollectionB but when creating, please click "Continue in Advanced Field Creation Mode" just below the "Save" button you'd like to rush to click otherwise: https://i.sstatic.net/DdvuJ.png

    Now, click "Relation" from the left menu and under "Corresponding Field" check the "Add M2M" to ColumnB" checkbox: https://i.sstatic.net/ZTnhQ.png

    If you don't want to delete what's created already (for example, lot of data in the tables already), then I think the only way is to make changes directly to the directus_* tables on the database. I don't know how to do it as I didn't have much data when I had this problem.