drizzle-team / drizzle-team/drizzle-orm

[BUG]: EmptyFilter cancels a relation filter's leaf predicates but not the relation's own EXISTS

Open
#6,217 1 comment 0 reactions 0 assignees View on GitHub
bug
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?

drizzle-orm@1.0.0-rc.5-ab785fc

### What version of `drizzle-kit` are you using?

-

### Other packages

_No response_

### Describe the Bug

EmptyFilter works for top-level column filters. It does not work when used for every field inside a relation sub-filter: the leaf predicates cancel, but the relation still emits an unconditional EXISTS (...) with no predicate, matching every row that merely has a related row.

Small example:

```ts
const foundEntries = await db.query.user.findMany({
where: {
OR: [
{ a: user.a ?? EmptyFilter },
{ b: user.b ?? EmptyFilter },
{ c: user.c ?? EmptyFilter },
...(user.email || user.username
? [
{
data: {
OR: [
{ email: user.email ?? EmptyFilter },
{ username: user.username ?? EmptyFilter },
],
},
},
]
: []),
],
},
with: {
data: true,
},
});
```
If `a`, `b` and `c` are all nullish and I would not perform the `user.email || user.username` check, this would occurr.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the nested relation-filter example in the issue with all relevant values nullish, then inspect the relation-filter path that turns EmptyFilter into SQL. Confirm the generated query does not retain an unconditional EXISTS when the relation has no remaining predicates; add coverage for this case and verify the existing top-level behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.