Querying relations
I have this schema:
Now I want to search for all the users in a group, so I write a query:
When I run this, I get the following error
What am I doing wrong? I'm explicitly selecting the columns I want and the generated sql looked right to me and I believe I've defined the relations correctly. Any help is greatly appreciated.
10 Replies
Generated sql for more context:
Hey 👋 I guess it is the example from the doc?
This is a demo with multiple ways https://drizzle.run/vmxbde9k7qh5g5gbtny7fl12
This is great! And if my users have roles which have permissions, can I include the permissions of the roles within my selection of the roles relationship?
Also for my understanding why was it trying to select(?) that column from my users table?
I have pushed a change on the demo but I only know how to do that the traditionnal way :/
hmm still running into this issue
Relation API is limited for complex queries. It will be far easier with joins
you can't join/reference other tables in where. It's good for one direct relation but not for checking against multiple conditions
hmm, still getting the same error with my initial query
where does the query get generated, maybe I can step through dn see what I'm doing wrong
hmmm, okay that confirms one of the tests I just did.
With the join approach, is it possible to select columns from the joined tables?
Looking at the roles example, lets say I want to get back
is that possible?
I was able to with the complex query. Only downside is I now need to manipulate the data into the shape I need.
If it is too complex with sql, do this mapping in JS/TS after you get the results of the query.
Yeah that's what I ended up doing.