© 2026 Hedgehog Software, LLC

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

Best way to get count in extras

await db.query.productSets
  .findMany({
    extras: {
      productsCount:
        sql`(SELECT count(*) from ${products} WHERE "Product"."productSetId" = ${productSets.id})`.as(
          'productsCount'
        )
    },
    with: { products: true }
  })
await db.query.productSets
  .findMany({
    extras: {
      productsCount:
        sql`(SELECT count(*) from ${products} WHERE "Product"."productSetId" = ${productSets.id})`.as(
          'productsCount'
        )
    },
    with: { products: true }
  })

is there a better way to do this without the
sql
sql
operator? I want to just use drizzle code (to make more complex queries). if i try to, i get weird issues. similarly, if i replace
"Product"."productSetId"
"Product"."productSetId"
with
${products.productSetId}
${products.productSetId}
, it tells me
productSetId
productSetId
does not exist on the ProductSet table (it uses the wrong table)
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

Bug: Count in Extras (Relational)
Drizzle TeamDTDrizzle Team / help
9mo ago
SQLite query returns an incorrect row count in extras
Drizzle TeamDTDrizzle Team / help
3y ago
good way to get the "count" for paginated queries
Drizzle TeamDTDrizzle Team / help
3y ago
Using extras in where
Drizzle TeamDTDrizzle Team / help
2y ago