How to filter by joined tables with Drizzle query syntax?
Hi there! I was wondering if there was a way to query by a joined table using drizzle query syntax. Here's a concise example:
So how is this supposed to be done? users <-> info is 1 to 1 so with
.select()
query I would just filter by the joined table like I'm trying above, but this is not working in this case. I can't add a where to the info block since it's a one to one.6 Replies
If you still need help, share the whole relevant schema.
Initial error that's immediately visible is the
db.users.findMany()
should be db.query.users.findMany()
@TOSL got it sorry yeah I just tried to simplify the example and that was a typo
I'll share exactly what I'm working with
Here is my query. In the conditions array you can see that I'm doing a subquery to compare leads.campaignId to the campaigns with organizationId that matches the one provided outside the scope of this block of code. With a
.select()
I could just query where campaigns.organizationId == organizationId but here I need the subquery.@Anthony In the above code you provided, where's the error being thrown? The above query looks very different from your stripped down example so I'm not sure.
Also, it may be relevant to know what conditions are/could be present in
customConditions
.You want to use a where condition that relies on the related table which the relation/query api has some restricting limitations in the current implementation. These limitations of reducing if not going away entirely in V2 on the api.
https://github.com/drizzle-team/drizzle-orm/discussions/2316
You can do something like
Basically, you use a subquery.
GitHub
Relational API v2 · drizzle-team drizzle-orm · Discussion #2316
We've launched Drizzle Relational Queries exactly a year ago and it's time to ship a fundamental upgrade. We've gathered a massive amount of valuable feedback from the community and goi...
Hi Mario, no error, basically instead of using the subquery I want to do a basic SQL command involving joining two tables A and B and filtering by B.column instead of A.column but I can't do this using this API, @TOSL this seems like a workaround not a solution.
What's your point? I already told you it's not directly possible using Drizzle's relation API. I gave you the only way to make it work.
Use select if you want direct solution