Skip to content

Commit 4c6cde6

Browse files
irvinzzymc9
andauthored
Reused existing relation name to produce opposite relation while expanding polymorphic relations (#1572)
Co-authored-by: Yiming <[email protected]> Co-authored-by: ymc9 <[email protected]>
1 parent 2760b41 commit 4c6cde6

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/schema/src/plugins/prisma/schema-generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ export class PrismaSchemaGenerator {
412412
const relAttr = getAttribute(field, '@relation');
413413
if (relAttr) {
414414
const fieldsArg = getAttributeArg(relAttr, 'fields');
415+
const nameArg = getAttributeArg(relAttr, 'name') as LiteralExpr;
415416
if (fieldsArg) {
416417
// for reach foreign key field pointing to the delegate model, we need to create an aux foreign key
417418
// to point to the concrete model
@@ -441,7 +442,7 @@ export class PrismaSchemaGenerator {
441442

442443
const addedRel = new PrismaFieldAttribute('@relation', [
443444
// use field name as relation name for disambiguation
444-
new PrismaAttributeArg(undefined, new AttributeArgValue('String', auxRelationField.name)),
445+
new PrismaAttributeArg(undefined, new AttributeArgValue('String', nameArg?.value || auxRelationField.name)),
445446
new PrismaAttributeArg('fields', fieldsArg),
446447
new PrismaAttributeArg('references', referencesArg),
447448
]);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
describe('issue 1575', () => {
3+
it('regression', async () => {
4+
await loadSchema(
5+
`
6+
model UserAssets {
7+
id String @id @default(cuid())
8+
videoId String
9+
videoStream Asset @relation("userVideo", fields: [videoId], references: [id])
10+
subtitleId String
11+
subtitlesAsset Asset @relation("userSubtitles", fields: [subtitleId], references: [id])
12+
}
13+
14+
model Asset {
15+
id String @id @default(cuid())
16+
type String
17+
userVideo UserAssets[] @relation("userVideo")
18+
userSubtitles UserAssets[] @relation("userSubtitles")
19+
20+
@@delegate(type)
21+
}
22+
23+
model Movie extends Asset {
24+
duration Int
25+
}
26+
`
27+
);
28+
});
29+
});

0 commit comments

Comments
 (0)