diff --git a/packages/drizzle/src/queries/sanitizeQueryValue.ts b/packages/drizzle/src/queries/sanitizeQueryValue.ts index 0e2ceedbd1f..eea9ae1842e 100644 --- a/packages/drizzle/src/queries/sanitizeQueryValue.ts +++ b/packages/drizzle/src/queries/sanitizeQueryValue.ts @@ -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, diff --git a/test/fields/int.spec.ts b/test/fields/int.spec.ts index 26412ed76f6..23b6a2f9289 100644 --- a/test/fields/int.spec.ts +++ b/test/fields/int.spec.ts @@ -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) + }) }) })