Skip to content

Commit d543a0d

Browse files
authored
fix(delegate): issue with filtering concrete relations with fields from base (#2260)
1 parent 237bb59 commit d543a0d

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

packages/runtime/src/enhancements/node/delegate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
228228
data[field] = {};
229229
}
230230
await this.injectSelectIncludeHierarchy(fieldInfo.type, data[field]);
231+
if (data[field].where) {
232+
this.injectWhereHierarchy(fieldInfo.type, data[field].where);
233+
}
231234
}
232235
}
233236

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue 2246', () => {
4+
it('regression', async () => {
5+
const { enhance } = await loadSchema(
6+
`
7+
model Media {
8+
id Int @id @default(autoincrement())
9+
title String
10+
mediaType String
11+
12+
@@delegate(mediaType)
13+
@@allow('all', true)
14+
}
15+
16+
model Movie extends Media {
17+
director Director @relation(fields: [directorId], references: [id])
18+
directorId Int
19+
duration Int
20+
rating String
21+
}
22+
23+
model Director {
24+
id Int @id @default(autoincrement())
25+
name String
26+
email String
27+
movies Movie[]
28+
29+
@@allow('all', true)
30+
}
31+
`
32+
);
33+
34+
const db = enhance();
35+
36+
await db.director.create({
37+
data: {
38+
name: 'Christopher Nolan',
39+
email: 'christopher.nolan@example.com',
40+
movies: {
41+
create: {
42+
title: 'Inception',
43+
duration: 148,
44+
rating: 'PG-13',
45+
},
46+
},
47+
},
48+
});
49+
50+
await expect(
51+
db.director.findMany({
52+
include: {
53+
movies: {
54+
where: { title: 'Inception' },
55+
},
56+
},
57+
})
58+
).resolves.toHaveLength(1);
59+
60+
await expect(
61+
db.director.findFirst({
62+
include: {
63+
_count: { select: { movies: { where: { title: 'Inception' } } } },
64+
},
65+
})
66+
).resolves.toMatchObject({ _count: { movies: 1 } });
67+
68+
await expect(
69+
db.movie.findMany({
70+
where: { title: 'Interstellar' },
71+
})
72+
).resolves.toHaveLength(0);
73+
74+
await expect(
75+
db.director.findFirst({
76+
include: {
77+
_count: { select: { movies: { where: { title: 'Interstellar' } } } },
78+
},
79+
})
80+
).resolves.toMatchObject({ _count: { movies: 0 } });
81+
});
82+
});

0 commit comments

Comments
 (0)