fix: hasMany text fields cannot be filtered with contains operator#15671
Merged
Conversation
Contributor
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖 |
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug where filtering hasMany text fields with the contains operator caused a TypeError in both MongoDB and Drizzle adapters. The issue occurred because the code attempted to call .replace() on array values without proper type checking.
Changes:
- Fixed MongoDB adapter to handle hasMany text/number/select fields with contains operator using appropriate query syntax
- Fixed Drizzle adapter to handle array values in hasMany fields by creating OR conditions with LIKE matching
- Added comprehensive integration and e2e tests for both single and multiple value contains queries
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/db-mongodb/src/queries/sanitizeQueryValue.ts | Added special handling for contains operator on hasMany text/number/select fields with regex matching |
| packages/drizzle/src/queries/sanitizeQueryValue.ts | Added array value handling for hasMany fields with LIKE operator wrapping |
| packages/drizzle/src/queries/parseParams.ts | Added OR condition logic for array values in hasMany contains queries |
| test/fields/int.spec.ts | Added integration tests for contains operator with single and multiple values, minor import reordering |
| test/fields/collections/Text/e2e.spec.ts | Added e2e tests for hasMany filtering in list view, minor import reordering |
| test/fields/payload-types.ts | Updated generated types to include richTextField in ArrayField (unrelated schema update) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
paulpopus
approved these changes
Feb 18, 2026
Contributor
|
🚀 This is included in version v3.77.0 |
AlessioGr
added a commit
that referenced
this pull request
Mar 6, 2026
…elds (#15865) Fixes `contains` operator on hasMany `select` fields in PostgreSQL. This was a regression introduced in #15671. ## The problem Given a collection with a hasMany `select` field: ```typescript { slug: 'users', fields: [ { name: 'roles', type: 'select', hasMany: true, options: ['user', 'admin', 'editor'], }, ], } ``` Payload stores this across two PostgreSQL tables. For a user created with `roles: ['admin', 'editor']`: **`users`** | id | created_at | updated_at | |----|------------|------------| | 1 | 2026-03-05 | 2026-03-05 | **`users_roles`** | id | parent_id | value | order | |----|-----------|----------|-------| | 1 | 1 | `admin` | 1 | | 2 | 1 | `editor` | 2 | The `value` column is a PostgreSQL **enum type**, not a `varchar`. When querying `{ roles: { contains: 'admin' } }`, Payload generated: ```sql SELECT DISTINCT "users"."id" FROM "users" LEFT JOIN "users_roles" ON "users"."id" = "users_roles"."parent_id" WHERE "users_roles"."value" ILIKE '%admin%' -- ❌ PostgreSQL error: operator does not exist: enum_users_roles ~~* unknown ``` `ILIKE` does not work on PostgreSQL enum types. ## The fix The LEFT JOIN already flattens the array into individual rows: | users.id | users_roles.value | |----------|-------------------| | 1 | `admin` | | 1 | `editor` | So "contains admin" just means "has a row where value equals admin". The fix converts `contains` to `equals` for hasMany `select` fields, the same way it already works for hasMany `relationship` and `upload` fields: ```sql SELECT DISTINCT "users"."id" FROM "users" LEFT JOIN "users_roles" ON "users"."id" = "users_roles"."parent_id" WHERE "users_roles"."value" = 'admin' -- ✅ Matches row 1, returns user id=1 ``` ## Why hasMany `text` fields are different HasMany `text` fields store values as `varchar`, not as an enum. `ILIKE` works fine on `varchar` columns, so substring matching (`contains: 'adm'` matching `'admin'`) is valid there. Whether it's _desirable_ is a problem separate from this PR, being discussed internally [here](https://payloadcms.slack.com/archives/C049BR3QBHC/p1772680331359859)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixed
containsoperator for hasMany text fields in MongoDB and Drizzle adapters.Why
Filtering hasMany text fields with
containsfailed withTypeError: formattedValue.replace is not a functionbecause the code didn't handle array values.How
$elemMatchwith regex to search within array elementsFixes #15662