drizzle-team / drizzle-team/drizzle-orm
[BUG]: Table names not referenced correctly in WHERE clause
- 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.39.3
### What version of `drizzle-kit` are you using?
0.30.4
### Other packages
_No response_
### Describe the Bug
In a query that references a sub table the where clause ends up using incorrect table names
[Drizzle playground repro](https://drizzle.run/xvbcbjh389b7goz5x25s48vl)
```ts
db.$count(
jobCandidacies,
and(
eq(jobCandidacies.candidateId, candidates.id),
not(
inArray(jobCandidacies.status, [
CandicacyStatus.CANCELED,
CandicacyStatus.CLOSED,
]),
),
),
)
.as("activeJobs"),
```
ends up generating SQL:
```sql
(
select
count(*)
from "job_candidacy"
where
("candidates"."candidate_id" = "candidates"."id" and not "candidates"."status" in ($1, $2))
) as "activeJobs"
```
- table is referenced correctly in the `from` clause but not in the `where` conditions where it picks up only the alias from the parent query
- resulting error message: `column candidates.candidate_id does not exist`
I can get around this by using raw SQL in the subquery but it is not a very good experience.
Any better solution or does this need fixing on the drizzle level?
Contributor guide
Assessment
This issue has not been assessed yet.