Symmetric many-to-many relationship in Directus
Asked Answered
P

1

5

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?

Parturient answered 25/12, 2022 at 9:36 Comment(0)
L
7

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.

Lindyline answered 25/12, 2022 at 12:33 Comment(2)
Thank you, this works as expected. I don't understand why this feature is not available easier. it seams that many-to-many relationships are widely used in symmetric manner, so it should be useful to add content from both collections.Parturient
Directus is pretty good compared to what it was about a year ago. It's a great project but still young. It still was pretty buggy here and there and much more stable today but still lacs lot of useful logic.Lindyline

© 2022 - 2024 — McMap. All rights reserved.