Skip to content

fix(db-postgres): querying with in: [] returns empty result - #11011

Closed
yunus-emre-zengin wants to merge 1 commit into
payloadcms:mainfrom
yunus-emre-zengin:main
Closed

yunus-emre-zengin wants to merge 1 commit into
payloadcms:mainfrom
yunus-emre-zengin:main

Conversation

@yunus-emre-zengin

@yunus-emre-zengin yunus-emre-zengin commented Feb 6, 2025 •

Copy link
Copy Markdown

There was an issue with relational databases where, when an empty array was used as a value for the IN operator, the constraint was being ignored. Fixes #10788

I investigated this issue and found that, in the past, Drizzle's inArray operator would throw an error when it received an empty array. Because of this behavior, the Payload server would crash when handling queries with in: []. To address this, the following commit was made: c4ac341.

However, this approach resulted in the constraint being ignored, which is not the ideal behavior. The Drizzle community discussed the expected behavior of the inArray operator (see: drizzle-team/drizzle-orm#1295), and the team decided to modify it to accept empty arrays instead of throwing an error.

With this change, simply removing the unnecessary check resolves the issue.

After making this fix, one of the tests failed because it was using the not_in operator. I also updated the test to reflect the new behavior.

@yunus-emre-zengin yunus-emre-zengin changed the title fix(db-postgres): querying with in: [] returns empty result #10788 fix(db-postgres): querying with in: [] returns empty result Feb 6, 2025
@yunus-emre-zengin

Copy link
Copy Markdown
Author

@r1tsuu I am mentioning you because the issue I was trying to solve is assigned to you and you are an active team member in the repo

I am not experienced with pull requests, did I miss something here? Several days passed but nothing happened

JarrodMFlesch added a commit that referenced this pull request Feb 10, 2025
### What?
If you had multiple operator constraints on a single field, the last one
defined would be the only one used.

Example:
```ts
where: {
  id: {
    in: [doc2.id],
    not_in: [], // <-- only respected this operator constraint
  },
}
```

and
```ts
where: {
  id: {
    not_in: [],
    in: [doc2.id], // <-- only respected this operator constraint
  },
}
```

They would yield different results.

### Why?
The results were not merged into an `$and` query inside parseParams.

### How?
Merges the results within an `$and` constraint.

Fixes #10944

Supersedes #11011
@r1tsuu

r1tsuu commented Feb 17, 2025

Copy link
Copy Markdown
Member

Thank you for the PR @yunus-emre-zengin!
Jarrod's PR #11087 was merged that fixes the issue and a bit more, thus I'm closing this. We appreciate your efforts!

@r1tsuu r1tsuu closed this Feb 17, 2025
kendelljoseph pushed a commit that referenced this pull request Feb 21, 2025
### What?
If you had multiple operator constraints on a single field, the last one
defined would be the only one used.

Example:
```ts
where: {
  id: {
    in: [doc2.id],
    not_in: [], // <-- only respected this operator constraint
  },
}
```

and
```ts
where: {
  id: {
    not_in: [],
    in: [doc2.id], // <-- only respected this operator constraint
  },
}
```

They would yield different results.

### Why?
The results were not merged into an `$and` query inside parseParams.

### How?
Merges the results within an `$and` constraint.

Fixes #10944

Supersedes #11011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected Behavior: in Operator Returns Entire Collection with Empty Array

2 participants