Skip to content

Commit 0e379f8

Browse files
authored
fix(schema): Provide a truncated name for unique constraints with polymorphic fk (#1999)
1 parent 317f535 commit 0e379f8

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {
2727
LiteralExpr,
2828
Model,
2929
NumberLiteral,
30-
ReferenceExpr,
3130
StringLiteral,
3231
} from '@zenstackhq/language/ast';
3332
import { getIdFields } from '@zenstackhq/sdk';
@@ -529,9 +528,15 @@ export class PrismaSchemaGenerator {
529528
if (found) {
530529
// replicate the attribute and replace the field reference with the new FK field
531530
const args: PrismaAttributeArgValue[] = [];
531+
const fieldNames: string[] = [];
532532
for (const arg of fields.items) {
533-
if (isReferenceExpr(arg) && arg.target.ref === origForeignKey) {
533+
if (!isReferenceExpr(arg)) {
534+
throw new PluginError(name, 'Unexpected field reference in @@unique attribute');
535+
}
536+
537+
if (arg.target.ref === origForeignKey) {
534538
// replace
539+
fieldNames.push(addedFkField.name);
535540
args.push(
536541
new PrismaAttributeArgValue(
537542
'FieldReference',
@@ -540,17 +545,21 @@ export class PrismaSchemaGenerator {
540545
);
541546
} else {
542547
// copy
548+
fieldNames.push(arg.target.$refText);
543549
args.push(
544550
new PrismaAttributeArgValue(
545551
'FieldReference',
546-
new PrismaFieldReference((arg as ReferenceExpr).target.$refText)
552+
new PrismaFieldReference(arg.target.$refText)
547553
)
548554
);
549555
}
550556
}
551557

558+
const constraintName = this.truncate(`${dataModel.name}_${fieldNames.join('_')}_unique`);
559+
552560
model.addAttribute('@@unique', [
553561
new PrismaAttributeArg(undefined, new PrismaAttributeArgValue('Array', args)),
562+
new PrismaAttributeArg('map', new PrismaAttributeArgValue('String', constraintName)),
554563
]);
555564
}
556565
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue 1992', () => {
4+
it('regression', async () => {
5+
await loadSchema(
6+
`
7+
enum MyAppUserType {
8+
Local
9+
Google
10+
Microsoft
11+
}
12+
13+
model MyAppCompany {
14+
id String @id @default(cuid())
15+
name String
16+
users MyAppUser[]
17+
18+
userFolders MyAppUserFolder[]
19+
}
20+
21+
model MyAppUser {
22+
id String @id @default(cuid())
23+
companyId String
24+
type MyAppUserType
25+
26+
@@delegate(type)
27+
28+
company MyAppCompany @relation(fields: [companyId], references: [id])
29+
userFolders MyAppUserFolder[]
30+
}
31+
32+
model MyAppUserLocal extends MyAppUser {
33+
email String
34+
password String
35+
}
36+
37+
model MyAppUserGoogle extends MyAppUser {
38+
googleId String
39+
}
40+
41+
model MyAppUserMicrosoft extends MyAppUser {
42+
microsoftId String
43+
}
44+
45+
model MyAppUserFolder {
46+
id String @id @default(cuid())
47+
companyId String
48+
userId String
49+
path String
50+
name String
51+
52+
@@unique([companyId, userId, name])
53+
@@unique([companyId, userId, path])
54+
55+
company MyAppCompany @relation(fields: [companyId], references: [id])
56+
user MyAppUser @relation(fields: [userId], references: [id])
57+
}
58+
`,
59+
{
60+
provider: 'postgresql',
61+
pushDb: false,
62+
}
63+
);
64+
});
65+
});

0 commit comments

Comments
 (0)