DT
Drizzle Team•3w ago
Raz

Bug: Count in Extras (Relational)

Trying to use the example from: https://orm.drizzle.team/docs/query-utils#count
const results = await context.db.query.label.findMany({
extras: {
postsCount: context.db.$count(danceLabel, eq(danceLabel.labelId, label.id)).as("postsCount"),
},

limit: input.limit,
offset: input.offset,
});
return results;
const results = await context.db.query.label.findMany({
extras: {
postsCount: context.db.$count(danceLabel, eq(danceLabel.labelId, label.id)).as("postsCount"),
},

limit: input.limit,
offset: input.offset,
});
return results;
NeonDbError: column "danceId" does not exist
NeonDbError: column "danceId" does not exist
it seems like the output SQL query is WRONG -- it is attempting to match on label.danceId instead of label.id
Drizzle ORM - Utils
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
6 Replies
Flo
Flo•3w ago
Getting the same kind of error.
return await db.query.projects.findFirst({
where: eq(projects.id, id),
with: {
tasks: {
extras: (taskFields) => ({
todoTotalCount: db.$count(taskTodos, eq(taskTodos.taskId, taskFields.id)).as('task_todo_total_count'),
todoFinishedCount: db.$count(taskTodos, eq(taskTodos.taskId, taskFields.id)).as('task_todo_total_count'),
})
}
}
});
return await db.query.projects.findFirst({
where: eq(projects.id, id),
with: {
tasks: {
extras: (taskFields) => ({
todoTotalCount: db.$count(taskTodos, eq(taskTodos.taskId, taskFields.id)).as('task_todo_total_count'),
todoFinishedCount: db.$count(taskTodos, eq(taskTodos.taskId, taskFields.id)).as('task_todo_total_count'),
})
}
}
});
PostgresError: column projects_tasks.task_id does not exist Anyone got a solution?
Raz
RazOP•3w ago
Glad to see im not the only one -- this looks easily repro-able, so hopefully a solution wont be too complex
Raz
RazOP•3w ago
GitHub
[BUG]: wrong table name generated in $count sub-expression · Iss...
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.39.3 What version of drizzle-kit...
GitHub
[BUG]: extras in findMany count relations is not working · Issue #...
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.35.2 What version of drizzle-kit...
GitHub
[BUG]: Query extras resolve table names incorrectly · Issue #3110 ...
What version of drizzle-orm are you using? 0.34.1 What version of drizzle-kit are you using? 0.25.0 Describe the Bug I'm running following query in my code: await db.query.contractors.findMany(...
GitHub
[BUG]: db.$count inside relational query "extras" generates SQL wit...
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.36.0 What version of drizzle-kit...
GitHub
$count · drizzle-team drizzle-orm · Discussion #3119
In recent release we've introduced $count API and I wanted to shine some light on why we decided to implement one. We pursue SQL-likeness with Drizzle, yet sometimes pure SQL is just bad, this ...
Flo
Flo•3w ago
Thank you for linking these, looked through them and found a solution in the comments (hardcoding the table/field names in the eq) :)
Raz
RazOP•3w ago
Do you know if we can do anything to get the maintainers to solve the actual issue for us?
Flo
Flo•3w ago
nope no idea 😅

Did you find this page helpful?