Skip to content

fix(orm): to-one relation filter missing delegate base join - #2771

Open
ymc9 wants to merge 1 commit into
devfrom
fix/issue-2768-delegate-base-relation-filter
Open

fix(orm): to-one relation filter missing delegate base join#2771
ymc9 wants to merge 1 commit into
devfrom
fix/issue-2768-delegate-base-relation-filter

Conversation

@ymc9

@ymc9 ymc9 commented Jul 27, 2026

Copy link
Copy Markdown
Member

Fixes #2768

Problem

Filtering a to-one relation whose target is a polymorphic subtype, by a field inherited from the delegate base, failed at the database:

error: missing FROM-clause entry for table "Resource"   // Postgres
no such column: Resource.locationId                     // SQLite
await db.employeeDeskSession.findMany({
  where: { desk: { locationId } },   // `desk` is a ResourceDesk; `locationId` lives on the Resource base
});

The generated SQL referenced the base table without joining it:

where exists (
  select 1 from "ResourceDesk" as "$$t1"
  where "$$t1"."id" = "EmployeeDeskSession"."deskId"
    and "Resource"."locationId" = ?   -- ❌ "Resource" is not in the FROM clause
)

Cause

BaseCrudDialect.buildToOneRelationFilter built its exists subquery with a bare selectFrom on the concrete model, so delegate base tables were never joined. buildFilter references base-inherited fields through fieldDef.originModel — qualified with the base model name — hence the dangling reference.

buildToManyRelationFilter already used buildSelectModel, which joins all delegate bases. That asymmetry is why some/none filters worked while the to-one form didn't.

Fix

Use buildSelectModel in the to-one path as well.

-const baseJoin = this.eb
-    .selectFrom(`${fieldDef.type} as ${joinAlias}`)
+const baseJoin = this.buildSelectModel(fieldDef.type, joinAlias)
     .select(this.eb.lit(1).as('_'))

Verification

  • New regression test tests/regression/test/issue-2768.test.ts — 4 cases. The three to-one cases (base FK field, base scalar with startsWith, base relation) each failed before this change; the to-many case is included to lock in the path that already worked.
  • Full regression suite: 154 files / 215 tests passed.
  • e2e ORM suite: 126 files / 1042 tests passed, no failures.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved filtering for relationships involving delegated models.
    • Ensured filters work correctly with inherited fields, scalar values, and nested relationships.
    • Corrected filtering for delegated one-to-many relationships using some and none conditions.
  • Tests

    • Added regression coverage for delegated relationship filtering scenarios.

Filtering a to-one relation whose target is a polymorphic subtype by a
field inherited from the delegate base failed with `missing FROM-clause
entry for table "<Base>"` (Postgres) / `no such column: <Base>.<field>`
(SQLite).

`buildToOneRelationFilter` built its `exists` subquery with a bare
`selectFrom` on the concrete model, so the delegate base tables were
never joined. `buildFilter` references base-inherited fields through
`fieldDef.originModel`, i.e. qualified with the base model name, so the
generated SQL named a table absent from the FROM clause.

Use `buildSelectModel` - the same helper `buildToManyRelationFilter`
already uses, which joins all delegate bases. That's why `some`/`none`
filters worked while the to-one form didn't.

Fixes #2768

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e0a48d2a-31ad-4301-8663-5c17fcf266ce

📥 Commits

Reviewing files that changed from the base of the PR and between fdf5616 and 3e3e647.

📒 Files selected for processing (2)
  • packages/orm/src/client/crud/dialects/base-dialect.ts
  • tests/regression/test/issue-2768.test.ts

📝 Walkthrough

Walkthrough

The to-one relation filter subquery now uses buildSelectModel to include delegated base-model joins. A regression suite validates delegated to-one filters on inherited fields and nested relations, plus to-many some and none filters.

Changes

Delegated relation filters

Layer / File(s) Summary
Relation filter join construction and regression coverage
packages/orm/src/client/crud/dialects/base-dialect.ts, tests/regression/test/issue-2768.test.ts
To-one relation EXISTS subqueries include delegate base joins, with tests covering inherited fields, nested relations, and to-many some/none conditions.

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

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: delegated to-one relation filtering now joins the base model.
Linked Issues check ✅ Passed The change addresses #2768 by fixing delegated to-one filtering on inherited base fields, and the regression tests cover the reported cases.
Out of Scope Changes check ✅ Passed The extra to-many regression coverage is still directly related to the same delegation filtering bug and not out of scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-2768-delegate-base-relation-filter

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.

❤️ Share

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

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.

Error: missing FROM-clause entry for table "Resource" (base entity)

1 participant