fix(orm): support _count nested inside an include#2712
Conversation
When `_count` appears inside a nested relation's `include` (e.g.
`include: { posts: { include: { _count: ... } } }`), the relation-JSON
builder treated it as a regular relation field. SQLite threw
`Field "_count" not found in model "Post"`, and Postgres/MySQL generated
a reference to a never-created lateral join (`missing FROM-clause entry`).
The `select` branch already special-cased `_count`; this applies the same
handling to the `include` branch in both dialects, calling `buildCountJson`.
Fixes #2669
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughTwo ORM dialect implementations ( Changes_count inside include fix
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Problem
Fixes #2669.
Using
_countinside a nested relation'sincludethrows at runtime:QueryError: Field "_count" not found in model "Post"missing FROM-clause entry for table "$$t3"This is valid Prisma syntax (verified against Prisma 6 — it returns
posts[]._count.comments), so it's a Prisma-compatibility gap.Root cause
The relation-JSON builder resolves a nested payload through two separate branches — one for
selectand one forinclude. Theselectbranch already special-cased_count(callingbuildCountJson), but theincludebranch did not:requireField(schema, 'Post', '_count')→ throws.$$..._count.$data) thatbuildRelationJoinsnever creates (it only joins real relation fields).Fix
Apply the same
_counthandling to theincludebranch in both dialects, callingbuildCountJsonwith the relation's alias so the count subquery correlates correctly:packages/orm/src/client/crud/dialects/sqlite.tspackages/orm/src/client/crud/dialects/lateral-join-dialect-base.tsWhy it wasn't caught
The e2e suite covered
_countat the top level ofselect/includeand_countnested inside a relation'sselect— all of which hit the (working) select path. No test nested_countinside aninclude, the one broken path.Tests
'supports _count nested inside an include'totests/e2e/orm/client-api/find.test.ts, covering_count: { select: ... },_count: true, and_countwith awherefilter on the counted relation. Verified it fails against the un-fixed build with the exact issue error and passes with the fix.tests/regression/test/issue-2669.test.ts(SQLite + Postgres).🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests