Skip to content

Commit a800fe6

Browse files
authoredOct 9, 2024··
fix: generated prisma schema contains error when using "@@unique" with base field (#1766)
1 parent 2d42606 commit a800fe6

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
 

‎packages/schema/src/language-server/validator/attribute-application-validator.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import {
1515
isEnum,
1616
isReferenceExpr,
1717
} from '@zenstackhq/language/ast';
18-
import { isDataModelFieldReference, isFutureExpr, isRelationshipField, resolved } from '@zenstackhq/sdk';
18+
import {
19+
isDataModelFieldReference,
20+
isDelegateModel,
21+
isFutureExpr,
22+
isRelationshipField,
23+
resolved,
24+
} from '@zenstackhq/sdk';
1925
import { ValidationAcceptor, streamAst } from 'langium';
2026
import pluralize from 'pluralize';
2127
import { AstValidator } from '../types';
@@ -164,6 +170,31 @@ export default class AttributeApplicationValidator implements AstValidator<Attri
164170
}
165171
}
166172

173+
@check('@@unique')
174+
private _checkUnique(attr: AttributeApplication, accept: ValidationAcceptor) {
175+
const fields = attr.args[0]?.value;
176+
if (fields && isArrayExpr(fields)) {
177+
fields.items.forEach((item) => {
178+
if (!isReferenceExpr(item)) {
179+
accept('error', `Expecting a field reference`, { node: item });
180+
return;
181+
}
182+
if (!isDataModelField(item.target.ref)) {
183+
accept('error', `Expecting a field reference`, { node: item });
184+
return;
185+
}
186+
187+
if (item.target.ref.$container !== attr.$container && isDelegateModel(item.target.ref.$container)) {
188+
accept('error', `Cannot use fields inherited from a polymorphic base model in \`@@unique\``, {
189+
node: item,
190+
});
191+
}
192+
});
193+
} else {
194+
accept('error', `Expected an array of field references`, { node: fields });
195+
}
196+
}
197+
167198
private validatePolicyKinds(
168199
kind: string,
169200
candidates: string[],
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { loadModelWithError } from '@zenstackhq/testtools';
2+
3+
describe('issue 1758', () => {
4+
it('regression', async () => {
5+
await expect(
6+
loadModelWithError(
7+
`
8+
model Organization {
9+
id String @id @default(cuid())
10+
contents Content[] @relation("OrganizationContents")
11+
}
12+
13+
model Content {
14+
id String @id @default(cuid())
15+
contentType String
16+
organization Organization @relation("OrganizationContents", fields: [organizationId], references: [id])
17+
organizationId String
18+
@@delegate(contentType)
19+
}
20+
21+
model Store extends Content {
22+
name String
23+
@@unique([organizationId, name])
24+
}
25+
`
26+
)
27+
).resolves.toContain('Cannot use fields inherited from a polymorphic base model in `@@unique`');
28+
});
29+
});

0 commit comments

Comments
 (0)
Please sign in to comment.