Skip to content

fix(drizzle): avoid fallback findFirst when deleteOne with joins finds no row - #19069

Open
MilindChatrabhuji wants to merge 1 commit into
payloadcms:mainfrom
MilindChatrabhuji:fix/drizzle-delete-one-joins-404-19044
Open

MilindChatrabhuji wants to merge 1 commit into
payloadcms:mainfrom
MilindChatrabhuji:fix/drizzle-delete-one-joins-404-19044

Conversation

@MilindChatrabhuji

Copy link
Copy Markdown

What?

Prevents deleteOne in the Drizzle adapter from falling back to findFirst when a query with joins returns no matching row.

Why?

In packages/drizzle/src/deleteOne.ts, when selectDistinct ran with joins and found no matching row (for instance, DELETE /api/payload-preferences/:key when the preference key does not exist), the else branch unconditionally fell back to db.query[tableName].findFirst(findManyArgs).

Because findManyArgs.where contained joined relationship conditions (e.g., 'user.value') but findFirst does not include joins, PostgreSQL failed with DrizzleQueryError: missing FROM-clause entry for table "<uuid>", returning an unexpected 500 status code instead of a 404.

How?

  • Aligned deleteOne.ts with updateOne.ts by only falling back to findFirst when !joins.length.
  • When joins are present and selectDistinct returns no rows, deleteOne cleanly returns null, allowing operations like deleteOperation in packages/payload/src/preferences/operations/delete.ts to properly throw NotFound.
  • Added unit tests in packages/drizzle/src/deleteOne.spec.ts covering:
    • Returning null without invoking findFirst when joins are present and no row matches.
    • Falling back to findFirst when no joins are present.
    • Deleting and returning the document when selectDistinct finds a matching row.

Fixes #19044

…s no row

In `packages/drizzle/src/deleteOne.ts`, when `selectDistinct` ran with joins and found no matching row (e.g. `DELETE /api/payload-preferences/:key` when the preference key does not exist), the `else` branch unconditionally fell back to `db.query[tableName].findFirst(findManyArgs)`. Because `findManyArgs.where` contained joined relationship conditions (such as `'user.value'`) but `findFirst` does not include joins, PostgreSQL failed with `DrizzleQueryError: missing FROM-clause entry for table "<uuid>"`, resulting in an unexpected 500 error instead of 404.

Align `deleteOne` with `updateOne` by only falling back to `findFirst` when `!joins.length`. When joins are present and `selectDistinct` returns no rows, `deleteOne` cleanly returns `null`. Added unit tests in `packages/drizzle/src/deleteOne.spec.ts`.

Fixes payloadcms#19044
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between aebbf75 and 62ab649.

Files selected for processing: 2
  • packages/drizzle/src/deleteOne.spec.ts
  • packages/drizzle/src/deleteOne.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

deleteOne now skips its fallback lookup when a joined distinct query returns no ID. Tests cover this case, the no-join fallback, and a joined query with a matching ID.

Changes: deleteOne fallback behavior

Layer / File(s) Summary
Fallback behavior and tests
packages/drizzle/src/deleteOne.ts, packages/drizzle/src/deleteOne.spec.ts
When a joined distinct query returns no ID, deleteOne skips the fallback findFirst call and returns null. Without joins, it retains the fallback. Tests also cover a matching joined ID and verify lookup and delete calls.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • payloadcms/payload#19044: Reports a 500 response when deleting a missing preference key. The issue describes the joined-query fallback that this change skips.

Possibly related PRs

Suggested reviewers: r1tsuu

Merge Risk: Minimal · up to 62ab6

No actionable issue is identified in the changed delete behavior or its tests; the PR is ready to merge after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description check Passed The description clearly explains the deleteOne bug, the join-related fallback behavior, the fix, and the added tests. It directly matches the changeset.
Title check Passed The title clearly and concisely describes the main change: preventing the findFirst fallback when deleteOne uses joins and finds no row.
Linked Issues check Passed The change satisfies issue #19044. deleteOne now skips the findFirst fallback when joins.length is nonzero, so joined no-match queries return null instead of issuing a query without the requir…
Out of Scope Changes check Passed The changes are limited to packages/drizzle/src/deleteOne.ts and its focused unit tests. The condition change and all test cases directly support issue #19044. No unrelated behavior or adapter chang…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Full details: Linked Issues check

Explanation

The change satisfies issue #19044. deleteOne now skips the findFirst fallback when joins.length is nonzero, so joined no-match queries return null instead of issuing a query without the required joins. The new tests cover this case, the no-join fallback, and deletion after a matching distinct result. The reported Postgres error path is therefore addressed.

Full details: Out of Scope Changes check

Explanation

The changes are limited to packages/drizzle/src/deleteOne.ts and its focused unit tests. The condition change and all test cases directly support issue #19044. No unrelated behavior or adapter changes are present.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DELETE /api/payload-preferences/:key returns 500 when the key does not exist (4.0.0-beta.4, Postgres)

1 participant