explain relationName please
Could anyone explain how to use relationName when you have many to many for example. Found following on github:
https://github.com/drizzle-team/drizzle-orm/issues/811
But doesn't explain how to use where. I have found some information about how to add it in relations, but not in the actual query.
GitHub
[FEATURE]: Allow alias for relational queries · Issue #811 · drizzl...
Describe what you want I have a use case where I need to query related data that is not always related by the same fields (i.e., it has different relations "types"), but I need to always ...
4 Replies
I'm gonna try and explain it based on my actual project.
Let's say I have a
users
table and facilities
table. I want a user to be able to create a new facility (be its owner) and to join an existing facility (be its member).
I will create two table schemas for that:
1. Facilities
2. Users
3. Users -> Facilities (many-to-many joined table)
And of course corresponding relations:
1. Facilities
2. Users
3. Users -> Facilities (many-to-many joined table relations)
Now, as you can see, our user will have two arrays of facilities in relations. One of them being ownedFacilities
, the second being joinedFacilities
. Drizzle does not have any way to actually distinguish them. You have to specify what relation belongs in which place. That's why we have to update our code and separate those 2 situations:
1. Facilities relations
2. Users relations
3. Users -> Facilities (many-to-many joined table relations)
I hope that's slightly more understandable now 🙂
@robboten
So, you don't use it on a query - this just tells Drizzle how to handle and distinguish multiple relations with the same entities
Prisma actually has the same system and if you want to give it a read in their docs here's the link: https://www.prisma.io/docs/concepts/components/prisma-schema/relations#disambiguating-relationsHere you go, I even made a small drawing of the situation 🙂
haha, thx!
Am trying to understand how to use with, so far some progress but not fully there. Especially with many to many I get the values from the junction table instead of the one I want to connect with on the other side. But will look thru your examples. Thank you again!
I think that many-to-many actually works that way - maybe I don't know something though haha
no problem!