{"record":{"id":"c3d4b6d9bef6e9fa","repo":"payloadcms/payload","slug":"only-equals-operator-is-supported-for-polymorphi","errorCode":null,"errorMessage":"Only 'equals' operator is supported for polymorphic relationship object notation. Given - ${operator}","messagePattern":"Only 'equals' operator is supported for polymorphic relationship object notation\\. Given - (.+?)","errorType":"validation","errorClass":"APIError","httpStatus":500,"severity":"error","filePath":"packages/drizzle/src/queries/sanitizeQueryValue.ts","lineNumber":157,"sourceCode":"      formattedValue = converted\n    }\n  }\n\n  if (field.type === 'relationship' || field.type === 'upload') {\n    if (val === 'null') {\n      formattedValue = null\n    } else if (!(formattedValue === null || typeof formattedValue === 'boolean')) {\n      // convert the value to the idType of the relationship\n      let idType: 'number' | 'text'\n      if (typeof field.relationTo === 'string') {\n        idType = getCollectionIdType({\n          adapter,\n          collection: adapter.payload.collections[field.relationTo],\n        })\n      } else {\n        if (isPolymorphicRelationship(val)) {\n          if (operator !== 'equals') {\n            throw new APIError(\n              `Only 'equals' operator is supported for polymorphic relationship object notation. Given - ${operator}`,\n            )\n          }\n          idType = getCollectionIdType({\n            adapter,\n            collection: adapter.payload.collections[val.relationTo],\n          })\n\n          if (isRawConstraint(val.value)) {\n            return {\n              operator,\n              value: val.value.value,\n            }\n          }\n          return {\n            operator,\n            value: idType === 'number' ? Number(val.value) : String(val.value),\n          }","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/drizzle/src/queries/sanitizeQueryValue.ts#L139-L175","documentation":"Thrown when querying a polymorphic relationship or upload field using the object notation (e.g. { relationTo: 'posts', value: '...' }) with any operator other than 'equals'. Polymorphic object notation resolves to a single (relationTo, value) pair, so only exact equality is meaningful; other operators (in, not_in, like, etc.) are rejected up front in sanitizeQueryValue before the SQL is built.","triggerScenarios":"A REST/Local/GQL API query against a polymorphic relationship field whose 'relationTo' is an array, passing an object value like { relationTo, value } together with an operator such as 'in', 'not_in', 'like', or 'exists' in the where clause.","commonSituations":"Trying to reuse a query shape that worked on a mono-collection relationship field against a polymorphic field; migrating a query from Mongo to the Drizzle adapter and keeping a multi-value operator; building a where clause dynamically and defaulting to 'in' for arrays of IDs.","solutions":["Switch the operator for that polymorphic condition to 'equals' and pass a single { relationTo, value } object.","If you need to match multiple IDs, query each relationTo separately or use an $or of multiple 'equals' conditions.","If you must use 'in', pass a flat array of IDs and ensure the field is not polymorphic, or restructure the schema so the relation targets a single collection."],"exampleFix":"// before\nwhere: {\n  relatedDocs: { in: [{ relationTo: 'posts', value: 'abc' }] }\n}\n// after\nwhere: {\n  relatedDocs: { equals: { relationTo: 'posts', value: 'abc' } }\n}","handlingStrategy":"validation","validationCode":"function buildPolymorphicWhere(field, operator, value) {\n  if (value && typeof value === 'object' && 'relationTo' in value && operator !== 'equals') {\n    throw new Error(`Polymorphic object notation requires operator 'equals', got '${operator}'`)\n  }\n  return { [field]: { [operator]: value } }\n}","typeGuard":"const isPolymorphicObjectValue = (v) => v !== null && typeof v === 'object' && 'relationTo' in v && 'value' in v","tryCatchPattern":"try {\n  await payload.find({ collection: 'docs', where })\n} catch (err) {\n  if (/Only 'equals' operator is supported for polymorphic/.test(err.message)) {\n    // rewrite the offending condition to use 'equals'\n  } else throw err\n}","preventionTips":["Centralize where-clause construction so polymorphic fields always use 'equals' with an object value.","Document which relationship fields are polymorphic in your schema so query authors know the constraint."],"tags":["query","polymorphic","relationship","validation"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}