fix: filtering by non-poly relationships with not_equals operator (#7573)

## Description

Fixes #5212

Fixes #6278 

- [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
This commit is contained in:
Patrik
2024-08-08 11:22:47 -04:00
committed by GitHub
parent 907d7d1d3a
commit efa56cefc1
4 changed files with 87 additions and 9 deletions

View File

@@ -31,6 +31,18 @@ const RelationshipFields: CollectionConfig = {
},
},
},
{
name: 'relationNoHasManyNonPolymorphic',
relationTo: 'text-fields',
type: 'relationship',
hasMany: false,
},
{
name: 'relationHasManyNonPolymorphic',
relationTo: 'text-fields',
type: 'relationship',
hasMany: true,
},
{
name: 'relationToSelf',
relationTo: relationshipFieldsSlug,

View File

@@ -244,9 +244,6 @@ describe('Fields', () => {
},
})
const anyChildren = await payload.find({
collection: relationshipFieldsSlug,
})
const allChildren = await payload.find({
collection: relationshipFieldsSlug,
where: {
@@ -1557,7 +1554,47 @@ describe('Fields', () => {
})
})
describe('relationships', () => {
describe('relationship queries', () => {
let textDoc
let otherTextDoc
let relationshipDocWithNonPolyOne
let relationshipDocWithNonPolyTwo
const textDocText = 'text document'
const otherTextDocText = 'alt text'
beforeEach(async () => {
textDoc = await payload.create({
collection: 'text-fields',
data: {
text: textDocText,
},
})
otherTextDoc = await payload.create({
collection: 'text-fields',
data: {
text: otherTextDocText,
},
})
const relationship = { relationTo: 'text-fields', value: textDoc.id }
relationshipDocWithNonPolyOne = await payload.create({
collection: relationshipFieldsSlug,
data: {
relationship,
relationNoHasManyNonPolymorphic: textDoc.id,
relationHasManyNonPolymorphic: textDoc.id,
},
})
relationshipDocWithNonPolyTwo = await payload.create({
collection: relationshipFieldsSlug,
data: {
relationship,
relationNoHasManyNonPolymorphic: otherTextDoc.id,
relationHasManyNonPolymorphic: otherTextDoc.id,
},
})
})
it('should not crash if querying with empty in operator', async () => {
const query = await payload.find({
collection: 'relationship-fields',
@@ -1570,6 +1607,25 @@ describe('Fields', () => {
expect(query.docs).toBeDefined()
})
it('should properly query non-polymorphic relationship with not equals', async () => {
const withoutHasMany = await payload.find({
collection: relationshipFieldsSlug,
where: {
relationNoHasManyNonPolymorphic: { not_equals: otherTextDoc.id },
},
})
const withHasMany = await payload.find({
collection: relationshipFieldsSlug,
where: {
relationHasManyNonPolymorphic: { not_equals: textDoc.id },
},
})
expect(withoutHasMany.docs).toHaveLength(1)
expect(withHasMany.docs).toHaveLength(1)
})
})
describe('clearable fields - exists', () => {

View File

@@ -955,6 +955,8 @@ export interface RelationshipField {
}
)[]
| null
relationNoHasManyNonPolymorphic?: (string | null) | TextField
relationHasManyNonPolymorphic?: (string | TextField)[] | null
relationToSelf?: (string | null) | RelationshipField
relationToSelfSelectOnly?: (string | null) | RelationshipField
relationWithDynamicDefault?: (string | null) | User