fix(db-postgres): count crashes when query contains subqueries and doesn't return any rows (#12273)

Fixes https://github.com/payloadcms/payload/issues/12264

Uses safe object access in `countDistinct`, fallbacks to `0`
This commit is contained in:
Sasha
2025-04-30 19:53:36 +03:00
committed by GitHub
parent 27d644f2f9
commit 4a56597b92
3 changed files with 38 additions and 4 deletions

View File

@@ -2451,4 +2451,37 @@ describe('database', () => {
expect(res.docs[0].id).toBe(customID.id)
})
it('should count with a query that contains subqueries', async () => {
const category = await payload.create({
collection: 'categories',
data: { title: 'new-category' },
})
const post = await payload.create({
collection: 'posts',
data: { title: 'new-post', category: category.id },
})
const result_1 = await payload.count({
collection: 'posts',
where: {
'category.title': {
equals: 'new-category',
},
},
})
expect(result_1.totalDocs).toBe(1)
const result_2 = await payload.count({
collection: 'posts',
where: {
'category.title': {
equals: 'non-existing-category',
},
},
})
expect(result_2.totalDocs).toBe(0)
})
})