Skip to content

Commit

Permalink
fix: filter with ID not_in AND queries - postgres (#6358)
Browse files Browse the repository at this point in the history
## Description

Fixes #5151 

`Issue`: With `Postgres`, when filtering by two queries with `AND`, if
the first query involved `ID` and the `not_in` operator, the second
query in the filter would never be evaluated.

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## Checklist:

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] Existing test suite passes locally with my changes
  • Loading branch information
PatrikKozak committed May 15, 2024
1 parent 1c30ad7 commit cc94078
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/db-postgres/src/queries/parseParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { GenericColumn, PostgresAdapter } from '../types'
import type { BuildQueryJoinAliases, BuildQueryJoins } from './buildQuery'

import { buildAndOrConditions } from './buildAndOrConditions'
import { convertPathToJSONTraversal } from './createJSONQuery/convertPathToJSONTraversal'
import { createJSONQuery } from './createJSONQuery'
import { convertPathToJSONTraversal } from './createJSONQuery/convertPathToJSONTraversal'
import { getTableColumnFromPath } from './getTableColumnFromPath'
import { operatorMap } from './operatorMap'
import { sanitizeQueryValue } from './sanitizeQueryValue'
Expand Down Expand Up @@ -195,10 +195,10 @@ export async function parseParams({
operator === 'not_in'
) {
constraints.push(
sql`${notInArray(table[columnName], queryValue)} OR
sql`(${notInArray(table[columnName], queryValue)} OR
${table[columnName]}
IS
NULL`,
NULL)`,
)

break
Expand Down
30 changes: 30 additions & 0 deletions test/versions/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,36 @@ describe('Versions', () => {

expect(byID.docs).toHaveLength(1)
})

it('should be able to query by id AND any other field with draft=true', async () => {
const allDocs = await payload.find({
collection: 'draft-posts',
draft: true,
})

expect(allDocs.docs.length).toBeGreaterThan(1)

const results = await payload.find({
collection: 'draft-posts',
draft: true,
where: {
and: [
{
id: {
not_in: allDocs.docs[0].id,
},
},
{
title: {
like: 'Published',
},
},
],
},
})

expect(results.docs).toHaveLength(1)
})
})

describe('Collections - GraphQL', () => {
Expand Down

0 comments on commit cc94078

Please sign in to comment.