`db.$count` with extras throws error when correlating with outer table

Hi! I'm trying to use .extras() with db.query.orderTable.findMany() to get the count of related orderItemTable rows like this:
const orders = await db.query.orderTable.findMany({
extras: (t) => ({
itemCount: db.$count(orderItemTable, eq(orderItemTable.orderId, t.id)),
}),
});
const orders = await db.query.orderTable.findMany({
extras: (t) => ({
itemCount: db.$count(orderItemTable, eq(orderItemTable.orderId, t.id)),
}),
});
But I get the following error:
column orderTable.order_id does not exist
column orderTable.order_id does not exist
It seems like orderTable.id is not available to the subquery context. I’ve followed the Drizzle docs (like how postsCount is used), but it still fails. My goal: add an itemCount column using a correlated subquery inside extras.
Drizzle ORM - Utils
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
2 Replies
Sillvva
Sillvva4mo ago
That's a SQL error that indicates that the table/column name you provided doesn't match what's in the database Check your drizzle schema and compare to your db table definitions If it matches correctly then there might be a bug
M Zeeshan
M ZeeshanOP4mo ago
hi @Sillvva , thanks for your precious time, i have reported
GitHub
[BUG]: extras does not qualify outer table columns, leading to inco...
Report hasn't been filed before. I have verified that the bug I'm about to report hasn't been filed before. What version of drizzle-orm are you using? ^0.44.2 What version of drizzle-ki...

Did you find this page helpful?