How to create M2X relationships.
In your Medium article (https://medium.com/directus/introducing-directus-7-8ea8ae288ec1) you mention :
New “Matrix” Relationships
Last but not least, in Directus 7 we’ve completely reimagined how we define schema relationships. Our new method elevates the relationship above the field level, thereby removing its “direction.” This makes it possible to define a relationship for a field and its counterpart can be enabled automatically.
This also opens up a unique type of relationship: the Many-to-Many-Many (M2MM, M2X, polymorphic relations), or matrix field. Now you can relate items from multiple collections through a single junction table, creating compound relationships linked by _UUIDs_.
Details on how to setup M2X relationships, using UUIDs.
(I believe detailed explanations on how to setup M2M relationships properly should also be added.)
I will gladly help writing the doc if I get more details.
Thanks for your work guys, Directus 7 is amazing <3
Hey @danyright – the M2X is essentially the same as a M2M... but we store a CSV of collections we're allowed to use within directus_relations.collection_one (instead of just a single collection name).
And since a primary key of 1 is ambiguous, we also force the use of UUIDs for the primary keys. One question that we haven't figured out yet is how we want to use the UUID to "find" the parent collection. I was thinking we could have a few different types of UUID:
uuid — standard, a normal UUID for anyone who wants to use this type.123e4567-e89b-12d3-a456-426655440000collection-uuid — prepends the collection name so that we can easily look up where a PK came from.collection_name:123e4567-e89b-12d3-a456-426655440000instance-uuid — this would be the ultimate PK but is not worth figuring out now. It would contain the instance (somehow), the collection name, and the standard UUID. This would allow us to create relationships across any web APIs!instance-name.com/_/:collection_name:123e4567-e89b-12d3-a456-426655440000Hi @benhaynes,
I only read the 2 first paragraphs of your message in my e-mail notification and actually got to the same conclusion you exposed.
I'm no SQL expert (rather dislike it because, to be honest, I doesn't really make sense anymore in many dev use cases) and I'm not sure I understand how you use the UUID to get back to the actual MySQL data.
But having a collection-uuid + eventually an instance-uuid makes sense.
And the reason why I'm event asking about this in the first place is because I tried to achieve exactly that the other day while playing around with directus :-)
Some purists might scream about performance issues, but I don't think this will matter before you have hundreds of thousands of entries...
One question though: why use a UUID in the first place?
Because if you have another field for the collection anyway, you will have to combine the two no matter what (unless I simply don't understand what you can do with your UUID).
Plus, will using UUIDs have an impact on indexing performance?
Good point! We progressed from UUID->prepending collection->interface level... but it's true: once you have a PK syntax like collection-name:123 you don't actually need to follow the UUID spec any more. You just end up with an _instance unique id_.
In fact, technically if you wanted to avoid changing your PK away from traditional INT IDs, we could just change how the junction table stores the relations. My only concern here is that we're moving more towards a "proprietary" relationship style since the FK and PK would differ.
I'm not sure about using a UUID as an index, depends if that index needs to re-sort after each new key is added. Either way, you could still SELECT from multiple tables without the prepended collection ... but I feel like this would be inefficient.
I'm no expert on all of this, I'm mostly in the conceptual realm. Expert opinions welcome! 😉
Probably not the best place to ask (and feel free to remove this comment), but, how would you create an m2x relationship? Would it be direct database modification? Or is there something I'm missing in the api?
We don't officially have M2X built-in yet, so you'd need to build this as a custom extension interface as of now...
Ah ok thank you for the response! Some of the language from the medium article
Now you can relate items from multiple collections through a single junction table, creating compound relationships linked by UUIDs.
And from the docs under Many-to-Any
The Directus relational architecture supports this type of relationship
Led me to believe Directus currently supports this feature. But after reading more issues (specifically directus/aip#8), I see this is not the case. I've only been digging into Directus for a few hours, will look into "custom extension interfaces" to see how easily this problem can be solved.
You're correct, we were building it as part of our last major release (alongside the Docs), but it didn't get merged in. We're working towards adding this, but can add a note to the Docs mentioning that this is not supported yet. Thanks!
Most helpful comment
You're correct, we were building it as part of our last major release (alongside the Docs), but it didn't get merged in. We're working towards adding this, but can add a note to the Docs mentioning that this is not supported yet. Thanks!