ยฉ 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Teamโ€ข16mo agoโ€ข
2 replies
gwilliamnn

With Query mode, is possible filter by a related table?

Is possible using db.query to filter (where) using a related table?

Example:
const users = await db.query.posts.findMany({
    where: (users, { eq }) => eq(posts.user.id, 1), // related user with post
})
const users = await db.query.posts.findMany({
    where: (users, { eq }) => eq(posts.user.id, 1), // related user with post
})


I know we can use with, but with will return the result and we can filter the with, I wanna filter the actual query using the related table.
We can do using the select
await db.select().from(posts).rightJoin(users, eq(users.id, posts.userId)).where(eq(posts.userId, users.id)
await db.select().from(posts).rightJoin(users, eq(users.id, posts.userId)).where(eq(posts.userId, users.id)
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to filter by a column in a related table?
Drizzle TeamDTDrizzle Team / help
3y ago
Query a table based on related table
Drizzle TeamDTDrizzle Team / help
3y ago
How to filter by joined tables with Drizzle query syntax?
Drizzle TeamDTDrizzle Team / help
14mo ago
can we filter by a prop on a relation during a query?
Drizzle TeamDTDrizzle Team / help
2y ago