© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
7 replies
cssetian

Trouble using findMany with joined relation and where clause on relation

Hello,

I'm trying to construct a query using the findMany function of drizzle, and perform a simple join to a second table with a foreign key relation and include a 'where' clause that keys on one of the columns of the joined table. My current query is constructed as follows:
  const response = await db.query.tableOne.findMany({
    where: and(gt(tableOne.createdAt, createdAfter), eq(tableOne.createdBy, userId)),
    with: {
      tableTwoEntity: {
        where: and(eq(tableTwo.organizationId, organizationId)),
      }
    },
    orderBy: [desc(tableOne.createdAt)],
    limit,
  })
  const response = await db.query.tableOne.findMany({
    where: and(gt(tableOne.createdAt, createdAfter), eq(tableOne.createdBy, userId)),
    with: {
      tableTwoEntity: {
        where: and(eq(tableTwo.organizationId, organizationId)),
      }
    },
    orderBy: [desc(tableOne.createdAt)],
    limit,
  })

I am querying against tableOne, which I have a relation to tableTwo defined for in my schema definition, with tableTwoEntity being the named entity of the relation. I would like to include a number of filters on tableOne, but additionally filter by tableTwo on one of the columns. For some reason Drizzle gives me a typescript error stating that the
Object literal may only specify known properties, and 'where' does not exist in type ...
Object literal may only specify known properties, and 'where' does not exist in type ...
. I believe this is the best practice way to do things, but would appreciate any guidance if I'm not following the correct best practice for doing this type of combined where clause in a findMany query. Thank you
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

Where clause on 'with' in findMany
Drizzle TeamDTDrizzle Team / help
17mo ago
Where clause dependent on with relation
Drizzle TeamDTDrizzle Team / help
2y ago
Using column of relation in where clause
Drizzle TeamDTDrizzle Team / help
2y ago
where clause when using 'with' in relational mode
Drizzle TeamDTDrizzle Team / help
2y ago