-
Notifications
You must be signed in to change notification settings - Fork 73
ManyToMany Relationship #328
Comments
Thank you so much @b1zzu. Ideally we need to have coverage for two cases:
|
@wtrocki both solutions look good, but we need to ensure that performance speaking one is not worse than the other and with this a mean that we should perform one single query in order to fetch the relation. For example, I have temporarily solved the many to many relation in this way, using an additional type as a join table
but this has the big disadvantage that if I want to query (graphql) one Activity with all the participant's name I will perform a db query for each participant user. Apart from this, I would prefer a ManyToMany relation out of the box, but it could be also interesting to have both solutions, especially in the case a user would like to have additional fields in the on the join relation. |
@ankitjena It looks like we have capabilities for Many2Many but they are not documented. However there seems to be a problem with how to model schema in order to support it. Resolvers in this case still using FK instead of relationship table. Is missing resolver support a reason for missing ManyToMany documentation? |
This was the reason why we removed it from documentation. We have not handled resolver implementation for this.
I think this will be good idea since we don't know what will be the user's usecases, and manytomany is not frequent. Suppose a case we need a ManyToMany relationship between User and Note, when implementing a custom resolver like |
I think we might go with:
Proposed suggestion seems to be quick wins but ManyToMany is so important feature that we might just do it properly. |
https://github.com/prisma/prisma2/blob/master/docs/relations.md#mn good read for possible approach |
The ManyToMany relation in Django is one of the best I've ever seen (https://docs.djangoproject.com/en/2.2/topics/db/examples/many_to_many/#) maybe we can take some ideas from it |
With the new migration engine integrated bringing ManyToMany table generation this moves one step closer. The Next steps: queries, mutations, subscriptions. [Requires further investigation] I think we would get away without any changes to the generated schema and that the majority of changes would happen at resolver generation phase. |
For many-to-many support at schema and resolver level, we have been considering letting users define two 1:M relationships and creating a many-to-many type in their model. AWS Amplify does something similar: aws-amplify/amplify-cli#91 (comment) |
Awesome! Then we should hide many2many on db as it will cause confusion |
We need documentation to cover this case and some example on top of the example apps to see if that approach will work |
Yep we should completely disable it. |
This can be closed since all that is required is documentation, which will come when we document relationships. |
Still not documented. User reported issue. Relationship documentation is hidden in wrong chapter https://graphback.dev/docs/db/dbmigrations#foreign-keys |
Relationship documentation is also in data model section: The bug isn't an issue with the documentation, but we should specify how to create a M:M |
Data model on is shorter - we are missing:
|
Adding ManyToMany now.
|
Oh.. I did not know about it. Good to add this to docs as well. |
Docs landed but I still think we need to have better support for this kind of relationship |
Agreed. I am trying out the suggested solutions in the doc in OVP, will try to gather some ideas. |
Closing as duplicate of #2023 |
Having a ManyToMany relationship could spare some boiler template work and also make the database more performant.
For example, I have this schema:
where a User can participate to has many Activities and an Activity can have many participants (Users).
This will be the expected graphql schema:
From the queries point of view, we can implement an optimized resolver for activities and participants, but they should be straight forward.
Instant for the mutations I have a couple of ideas:
And finally in the database should be created a table called
activieties_to_partecipants
with theuserId
andactivityId
foreign keys, and usually, the id would be the combination of the two foreign keys, but it's not always like that.It's also important to keep in mind that it could be useful to personalize the DB table name, the foreign keys names, and the mutations names (if using the first option)
The text was updated successfully, but these errors were encountered: