Repository navigation
fix(drizzle): avoid fallback findFirst when deleteOne with joins finds no row - #19069
MilindChatrabhuji wants to merge 1 commit into
Conversation
…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
|
No actionable comments were generated in the recent review.
|
| 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:
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
- payloadcms/payload#19048: Also changes
deleteOnebehavior when a joinedselectDistinctquery returns no row.
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: 
5
-
Full details: Linked Issues check
-
Explanation
The change satisfies issue
#19044.deleteOnenow skips thefindFirstfallback whenjoins.lengthis nonzero, so joined no-match queries returnnullinstead 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.tsand 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.
What?
Prevents
deleteOnein the Drizzle adapter from falling back tofindFirstwhen a query with joins returns no matching row.Why?
In
packages/drizzle/src/deleteOne.ts, whenselectDistinctran with joins and found no matching row (for instance,DELETE /api/payload-preferences/:keywhen the preference key does not exist), theelsebranch unconditionally fell back todb.query[tableName].findFirst(findManyArgs).Because
findManyArgs.wherecontained joined relationship conditions (e.g.,'user.value') butfindFirstdoes not include joins, PostgreSQL failed withDrizzleQueryError: missing FROM-clause entry for table "<uuid>", returning an unexpected 500 status code instead of a 404.How?
deleteOne.tswithupdateOne.tsby only falling back tofindFirstwhen!joins.length.selectDistinctreturns no rows,deleteOnecleanly returnsnull, allowing operations likedeleteOperationinpackages/payload/src/preferences/operations/delete.tsto properly throwNotFound.packages/drizzle/src/deleteOne.spec.tscovering:nullwithout invokingfindFirstwhen joins are present and no row matches.findFirstwhen no joins are present.selectDistinctfinds a matching row.Fixes #19044