Skip to content

sql-orm-client: relation predicate (some/every/none) returns wrong results on self-referential relations #980

Description

@SevInf

Summary

A relation predicate (some / every / none) evaluated against a self-referential one-to-many relation returns incorrect results. A some predicate that should match a parent row returns an empty set.

Ordinary (non-self) relation predicates work correctly, and include() on the same self-referential relation works correctly — only the relation predicate on a self relation is affected. This points to a table-alias collision in the generated EXISTS subquery when the parent and child resolve to the same underlying table.

Reproduction

Fixture: User { id, name, email, invitedById } with a self relation User.invitedUsers (1-m via invited_by_id) and User.invitedBy (m-1).

Seed: Alice(id=1, invitedById=null), Bob(id=2, invitedById=1), Eve(id=3, invitedById=null), Frank(id=4, invitedById=3).

const rows = await users
  .where((u) => u.invitedUsers.some((invitee) => invitee.name.eq('Bob')))
  .orderBy((u) => u.id.asc())
  .all();
  • Expected: [Alice] (Alice invited Bob).
  • Actual: [].

Evidence this is a self-join aliasing issue

  • some / every / none on ordinary (non-self) one-to-many relations return correct results.
  • include('invitedUsers', ...) on this exact self relation returns correct results.
  • Only the combination — a relation predicate on a self-referential relation — fails.

Failing test

test/integration/test/sql-orm-client/ported-regressions.test.ts — test title entry 107: some filter on a self-referential one-to-many relation.

The test is annotated with it.fails(...) referencing this issue so the suite stays green while the bug is open. Remove the .fails annotation once the bug is fixed and confirm the test passes.

Provenance

Found while porting upstream Prisma engine/client test behaviours into prisma-next (tests-ports.md, entry 107). Related upstream coverage: query-engine-tests/tests/queries/filters/many_relation.rs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions