Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/drizzle/src/queries/sanitizeQueryValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,22 @@ export const sanitizeQueryValue = ({
}
}

if ((field.type === 'relationship' || field.type === 'upload') && Array.isArray(formattedValue)) {
if (operator === 'equals') {
return {
columns: formattedColumns,
operator: 'in',
value: formattedValue,
}
} else if (operator === 'not_equals') {
return {
columns: formattedColumns,
operator: 'not_in',
value: formattedValue,
}
}
}

return {
columns: formattedColumns,
operator,
Expand Down
38 changes: 38 additions & 0 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4149,6 +4149,44 @@ describe('Fields', () => {
expect(noMatchDocIDs).toContain(relDoc1.id)
expect(noMatchDocIDs).not.toContain(relDoc2.id)
})

it('should not throw when querying hasMany relationship with equals array', async () => {
const text1 = await payload.create({
collection: 'text-fields',
data: { text: 'Text 1' },
})

const relDoc = await payload.create({
collection: 'relationship-fields',
data: {
relationshipHasMany: [text1.id],
relationship: { relationTo: 'text-fields', value: text1.id },
},
})
createdIDs.push(relDoc.id)

const equalsResult = await payload.find({
collection: 'relationship-fields',
where: {
relationshipHasMany: {
equals: [text1.id],
},
},
})

expect(equalsResult.docs.some((doc) => doc.id === relDoc.id)).toBe(true)

const notEqualsResult = await payload.find({
collection: 'relationship-fields',
where: {
relationshipHasMany: {
not_equals: [text1.id],
},
},
})

expect(notEqualsResult.docs.some((doc) => doc.id === relDoc.id)).toBe(false)
})
})
})

Expand Down
Loading