Skip to content

Commit

Permalink
Reused existing relation name to produce opposite relation while expa…
Browse files Browse the repository at this point in the history
…nding polymorphic relations (#1572)

Co-authored-by: Yiming <[email protected]>
Co-authored-by: ymc9 <[email protected]>
  • Loading branch information
3 people authored Jul 12, 2024
1 parent 2760b41 commit 4c6cde6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export class PrismaSchemaGenerator {
const relAttr = getAttribute(field, '@relation');
if (relAttr) {
const fieldsArg = getAttributeArg(relAttr, 'fields');
const nameArg = getAttributeArg(relAttr, 'name') as LiteralExpr;
if (fieldsArg) {
// for reach foreign key field pointing to the delegate model, we need to create an aux foreign key
// to point to the concrete model
Expand Down Expand Up @@ -441,7 +442,7 @@ export class PrismaSchemaGenerator {

const addedRel = new PrismaFieldAttribute('@relation', [
// use field name as relation name for disambiguation
new PrismaAttributeArg(undefined, new AttributeArgValue('String', auxRelationField.name)),
new PrismaAttributeArg(undefined, new AttributeArgValue('String', nameArg?.value || auxRelationField.name)),
new PrismaAttributeArg('fields', fieldsArg),
new PrismaAttributeArg('references', referencesArg),
]);
Expand Down
29 changes: 29 additions & 0 deletions tests/regression/tests/issue-1575.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1575', () => {
it('regression', async () => {
await loadSchema(
`
model UserAssets {
id String @id @default(cuid())
videoId String
videoStream Asset @relation("userVideo", fields: [videoId], references: [id])
subtitleId String
subtitlesAsset Asset @relation("userSubtitles", fields: [subtitleId], references: [id])
}
model Asset {
id String @id @default(cuid())
type String
userVideo UserAssets[] @relation("userVideo")
userSubtitles UserAssets[] @relation("userSubtitles")
@@delegate(type)
}
model Movie extends Asset {
duration Int
}
`
);
});
});

0 comments on commit 4c6cde6

Please sign in to comment.