Repository navigation
Description
Describe the Bug
On Postgres and SQLite, like and contains read % and _ in the search value as LIKE wildcards. On Postgres, \ also acts as the LIKE escape character. MongoDB matches the same value literally. So the same query returns different documents on different adapters.
The docs describe like as "Case-insensitive string must be present" and contains as "Must contain the value entered". The admin list search uses like. On Postgres and SQLite, a search for _ returns every document with a non-empty title, and a search for % returns all documents.
Result with two posts per case, where: { title: { like: <value> } }. contains gives the same result:
| Search value | Expected | Postgres 17.10 | SQLite | MongoDB 7.0.14 |
|---|---|---|---|---|
50% |
50% off |
50% off, 500 off |
50% off, 500 off |
50% off |
a_b |
a_b |
a_b, axb |
a_b, axb |
a_b |
a\b |
a\b |
ab |
a\b |
a\b |
Cause: the Drizzle adapter builds the pattern as `%${value}%` without escaping the value:
packages/drizzle/src/queries/parseParams.ts:like, one pattern per wordpackages/drizzle/src/find/buildPolymorphicJoinWhere.ts:likein polymorphic joinspackages/drizzle/src/queries/sanitizeQueryValue.ts:contains
The MongoDB adapter escapes the value with escapeRegExp (packages/db-mongodb/src/queries/buildSearchParams.ts, sanitizeQueryValue.ts).
The existing test like - special characters in test/collections-rest/int.spec.ts includes % and _. But it creates only one post, so it cannot see a query that matches too much.
The same code is on 3.x @ 9106bf8c1.
I have a fix with a test and can open a PR against main, and a backport to 3.x if you want one.
Link to the code that reproduces this issue
Reproduction Steps
The linked commit adds a test to test/collections-rest/int.spec.ts. Run it on main @ 5448061ac:
pnpm install
PAYLOAD_DATABASE=sqlite pnpm test:int collections-rest -t "wildcard characters match literally"
PAYLOAD_DATABASE=postgres POSTGRES_URL=postgres://[email protected]:5433/payload \
pnpm test:int collections-rest -t "wildcard characters match literally"I ran Postgres without Docker, on a local PostgreSQL 17.10 with PostGIS. Set POSTGRES_URL to your own database.
- SQLite: 4 of 6 fail (
%and_). - Postgres: 6 of 6 fail.
- MongoDB: 6 of 6 pass.
Without the test, on any Postgres database:
select 'axb' ilike '%a_b%', '500 off' ilike '%50%%', 'a\b' ilike '%a\b%';
-- t | t | fWhich area(s) are affected?
db: postgres, db: sqlite
Environment Info
payload: main @ 5448061ac (monorepo), 3.x @ 9106bf8c1 has the same code
Node.js: 24.14.1
pnpm: 11.9.0
PostgreSQL: 17.10 with PostGIS 3.6.4
SQLite: @libsql/client from the monorepo lockfile
MongoDB: 7.0.14 (control)
OS: macOS 25.6, arm64
Activity