Filtering Entire Objects by Related Records in Drizzle ORM Query API
Hi,
Is there currently functionality with Drizzle's query API to filter entire objects based on the contents of their relationships, rather than just filtering the related objects themselves. Specifically, I want to filter the main object (sourceTable) based on a condition in its related object (userToSource).
Here is an example of what I'm currently doing:
In this example, I want the entire sourceTable object to be returned only if the userToSource relationship meets the specified condition (userToSource.userId === user.id). However, it seems that the current functionality only filters the userToSource relationship itself, not the main sourceTable object.
Thank in advance!
6 Replies
Is the current best implementation to just filter out the sources variable after the fact?
Try a where subquery on the source table
share schema for more help
Can you please elaborate on what you mean by this?
Here is my schema
bump
So if I understand you correctly, you want to use a where condition that relies on the related table which the relation/query api has some restricting limitations on in the current implementation. These limitations are reducing if not going away entirely in V2 on the api.
https://github.com/drizzle-team/drizzle-orm/discussions/2316
For now, you can do something like (not exact for your use case I'm just pasting in the same example I gave someone else)
Your use case might look something like:
Probably not exactly right so you'll have to tinker
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...
Drizzle ORM - Select
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Ok thanks! This was very helpful.