drizzle-team / drizzle-team/drizzle-orm

[BUG]: db.$count inside relational query "extras" generates SQL with incorrect table names

Open
#3,493 26 comments 53 reactions 0 assignees View on GitHub
bug bug/fixed-in-beta priority qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [X] 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` are you using?

0.27.1

### Other packages

_No response_

### Describe the Bug

Following the docs for the `db.$count` helper, I wanted to use it inside a relational query to also get a count of some related entity. E.g querying for users, and including a count of the posts related to them.

This is shows in the docs by using `db.$count` within the `extras` field of the query: https://orm.drizzle.team/docs/query-utils#count, https://orm.drizzle.team/docs/select#count. (The docs also forget to include that you need to chain `db.$count(...).as('countAlias')` for use in `extras`)

Minimal repro: https://stackblitz.com/edit/stackblitz-starters-oo7in9?file=index.mjs

Simple schema: users table with `id`, posts table with `id`, `user`.

Relational query:
```ts
var q = db.query.user.findMany({
extras: {
postCount: db.$count(post, eq(post.user, user.id)).as('postCount'), // Needed to add this `.as()` which wasn't in the docs
},
});

console.log(q.toSQL()); // All table names in the query SQL are incorrectly aliased to the table alias of the table the relational query is being generated for, in this case "user". So the $count sql contains 'where "user"."user"' instead of 'where "post"."user"'
```

Resulting SQL:
```sql
select "id", (select count(*) from "post" where "user"."user" = "user"."id") as "postCount" from "user"
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.