Search code examples
phprelationshipdirectusheadless-cms

Directus : How to create a many-to-many relationship to the same collection properly


I created a topics collection. And I want to create a many-to-many relationship to the same collection. The column name is related_topics.

I followed the documentation so I created a many-to-many relation for this column and a junction table which will relate topic.id to topic_related.topic_id1 and from the other side topic.id to topic_related.topic_id2.

For a Topic (A) I can select the Topic (B).

However if I open (B) I don't see any related topic there.

I expect to see (A).

Am I doing something wrong ?

Thanks for your time !

EDIT1 :

Related Topic enter image description here

On the page of topic A I will select B as related article : enter image description here

I expect to find this relation on the page of topic B also but I found this : enter image description here


Solution

  • There seems to be a discrepancy in the description as I can understand, but, is this what you are trying to do:

    topics.related_topics => junction collection => topics.id
    

    If so, you have created a unidirectional relation from parent to one or more children on the field related_topics, in that you are asking what topics are related to this item.

    If you want to see to what parents a child item is related, create another M2M Interface using the same junction collection, but flip the fields from the previous M2M Interface settings and now you can see it the other way automatically.

    You should then be doing:

    topics.id => junction collection => topics.related_topics
    

    The logic of M2M is unidirectional: parent to many children as it extends O2M: https://docs.directus.io/guides/interfaces.html#core-interfaces

    You can, however, extend the M2M Interface if you want to make it bidirectional by following, intermediate understanding of Vue.js is recommended: https://docs.directus.io/extensions/interfaces.html

    You would need to update the input.vue in: https://github.com/directus/app/tree/master/src/interfaces/many-to-many