drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.61k stars 649 forks source link

[BUG]: extras in findMany count relations is not working #3546

Open manuchekhr32 opened 1 week ago

manuchekhr32 commented 1 week ago

Report hasn't been filed before.

What version of drizzle-orm are you using?

0.35.2

What version of drizzle-kit are you using?

0.26.2

Other packages

No response

Describe the Bug

Let's assume I have Batches and BatchItems of Batches (one to many). I want to find all batches with the count of batchItems in each batches in query.findMany. I got this from the docs and it's clearly not working: image I tried that: Using db.$count()

extras: {
  itemsCount: this.db
      .$count(batchItems, eq(batchItems.batchId, batches.id))
      .as('itemsCount'),
}

But got that error: PostgresError: column batches.batch_id does not exist

It's working if I do this:

itemsCount: this.db
  .$count(batchItems, sql`batch_items.batch_id = batches.id`)
  .as('itemsCount'),

But it's not a good way and why the first method is changing the table name? Btw, itemsCount gives a string not a number used .mapWith(Number) didn't work