Skip to content

Commit bc2d4b9

Browse files
committed
fix(sql-orm-client): namespace-qualify M:N filter EXISTS after TML-2605 rebase
The EXISTS-through-junction subquery built bare table sources, which no longer compiled (main dropped the raw TableSource import) and rendered unqualified SQL. Qualify the target via tableSourceForContract and the junction via the carried through.namespaceId. Signed-off-by: Alexey Orlenko's AI Agent <robot@aqrln.net>
1 parent 22d77a3 commit bc2d4b9

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

packages/3-extensions/sql-orm-client/src/model-accessor.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
JoinAst,
1212
ProjectionItem,
1313
SelectAst,
14+
TableSource,
1415
} from '@prisma-next/sql-relational-core/ast';
1516
import { codecRefForStorageColumn } from '@prisma-next/sql-relational-core/codec-descriptor-registry';
1617
import type { Expression, ScopeField } from '@prisma-next/sql-relational-core/expression';
@@ -341,8 +342,13 @@ function buildManyToManyExistsExpr<TContract extends Contract<SqlStorage>>(
341342
}
342343

343344
const firstTargetCol = through.targetColumns[0] ?? 'id';
344-
const subquery = SelectAst.from(TableSource.named(relatedTableName))
345-
.withJoins([JoinAst.inner(TableSource.named(junctionTable), junctionJoinOn)])
345+
const subquery = SelectAst.from(tableSourceForContract(context.contract, relatedTableName))
346+
.withJoins([
347+
JoinAst.inner(
348+
TableSource.named(junctionTable, undefined, through.namespaceId),
349+
junctionJoinOn,
350+
),
351+
])
346352
.withProjection([ProjectionItem.of('_exists', ColumnRef.of(relatedTableName, firstTargetCol))])
347353
.withWhere(subqueryWhere);
348354

packages/3-extensions/sql-orm-client/test/model-accessor.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,10 @@ describe('createModelAccessor', () => {
501501
const expr = accessor['children']!.some() as ExistsExpr;
502502

503503
expect(expr.notExists).toBe(false);
504-
expect(expr.subquery.from).toEqual(TableSource.named('children'));
504+
expect(expr.subquery.from).toEqual(TableSource.named('children', undefined, 'public'));
505505
expect(expr.subquery.joins).toEqual([
506506
JoinAst.inner(
507-
TableSource.named('parent_children'),
507+
TableSource.named('parent_children', undefined, 'public'),
508508
BinaryExpr.eq(
509509
ColumnRef.of('parent_children', 'child_id'),
510510
ColumnRef.of('children', 'id'),
@@ -631,7 +631,7 @@ describe('createModelAccessor', () => {
631631

632632
expect(expr.subquery.joins).toEqual([
633633
JoinAst.inner(
634-
TableSource.named('parent_children'),
634+
TableSource.named('parent_children', undefined, 'public'),
635635
AndExpr.of([
636636
BinaryExpr.eq(
637637
ColumnRef.of('parent_children', 'tenant_id'),

0 commit comments

Comments
 (0)