© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago
pastilhas

Help with nested query

Hey everyone,
I have the following query and it works as expected.

const companies = await db.query.Companies.findMany({
  where: and(eq(Companies.parentCompanyId, 0), notInArray(Companies.id, existingCompanyIds)),
  with: {
    users: {
      where: eq(Users.role, 'Owner'),
      limit: 1,
      orderBy: asc(Users.createdAt),
    },
  },
});
const companies = await db.query.Companies.findMany({
  where: and(eq(Companies.parentCompanyId, 0), notInArray(Companies.id, existingCompanyIds)),
  with: {
    users: {
      where: eq(Users.role, 'Owner'),
      limit: 1,
      orderBy: asc(Users.createdAt),
    },
  },
});


I would like to improve it in order to only return the companies that have one or more users with the role 'Owner'
so, if the amount of users returned is zero, not return that company.

Is it possible? How?

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

Help needed for Nested Query
Drizzle TeamDTDrizzle Team / help
2y ago
nested query
Drizzle TeamDTDrizzle Team / help
3y ago
DEEPLY NESTED QUERY using { with }
Drizzle TeamDTDrizzle Team / help
2y ago