fix(drizzle): apply joins to count subquery in join field traversal #13985
+78
−7
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.
Fixes a critical issue where count subqueries for join fields were missing necessary JOINs, causing database errors when WHERE clauses referenced columns from joined tables.
Problem
After PR #11923, the main query for join fields correctly applies joins using
joins.forEach()
, but the count subquery was constructed without applying the same joins. This caused errors like:When using join fields with count queries:
Solution
Applied the same join logic to count subqueries that was already being used for the main query. The count subquery now includes all necessary JOINs before creating the count operation.
Before:
After:
Testing
This change ensures that count subqueries for join fields properly include all necessary JOINs, resolving database errors when WHERE clauses reference columns from joined tables.
Fixes #[issue-number]
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
fastdl.mongodb.org
node ./postinstall.js
(dns block)node /home/REDACTED/work/payload/payload/node_modules/.bin/../jest/bin/jest.js --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand test/joins/int.spec.ts -t should count joins --testTimeout=30000
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>Count subquery for "join field" forgot to join after PR #11923</issue_title>
<issue_description>### Describe the Bug
The original implementation in PR payloadcms/payload#11395 correctly counted related docs. But after PR payloadcms/payload#11923, the count subquery for join fields no longer includes the necessary JOINs.
Current Problem
PR #11923 updated document retrieval queries to include joins.forEach after building the query:
This works for retrieving documents. But the same logic was not applied to the count subquery, leaving it without the necessary joins.
Finding document like this
yields error
Suggested Fix
Update the count subquery to also include the joins:
There may be other cases of this issue introduced from PR 11923, but I haven't bothered to check.
Link to the code that reproduces this issue
https://github.com/NadhifRadityo/payload-issue-count-subquery-does-not-include-proper-joins
Reproduction Steps
pnpm install
pnpm run dev
Which area(s) are affected? (Select all that apply)
area: core, db-postgres, db-sqlite, db-vercel-postgres
Environment Info