Skip to content

Commit

Permalink
fix: wrong validation error when relation field is marked @id instead…
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored May 5, 2023
1 parent ef7acd7 commit 9a18af6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/schema/src/language-server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export function getIdFields(model: DataModel) {
* Gets lists of unique fields declared at the data model level
*/
export function getUniqueFields(model: DataModel) {
const uniqueAttrs = model.attributes.filter((attr) => attr.decl.ref?.name === '@@unique');
const uniqueAttrs = model.attributes.filter(
(attr) => attr.decl.ref?.name === '@@unique' || attr.decl.ref?.name === '@@id'
);
return uniqueAttrs.map((uniqueAttr) => {
const fieldsArg = uniqueAttr.args.find((a) => a.$resolvedParam?.name === 'fields');
if (!fieldsArg || !isArrayExpr(fieldsArg.value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export default class DataModelValidator implements AstValidator<DataModel> {
thisRelation.fields?.forEach((ref) => {
const refField = ref.target.ref as DataModelField;
if (refField) {
if (refField.attributes.find((a) => a.decl.ref?.name === '@unique')) {
if (refField.attributes.find((a) => a.decl.ref?.name === '@id' || a.decl.ref?.name === '@unique')) {
return;
}
if (uniqueFieldList.some((list) => list.includes(refField))) {
Expand Down
61 changes: 61 additions & 0 deletions tests/integration/tests/regression/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,65 @@ describe('GitHub issues regression', () => {
expect(queried.posts[0].zenstack_guard).toBeUndefined();
expect(queried.posts[0].zenstack_transaction).toBeUndefined();
});

it('issue 392', async () => {
await loadSchema(
`
model M1 {
m2_id String @id
m2 M2 @relation(fields: [m2_id], references: [id])
}
model M2 {
id String @id
m1 M1?
}
`
);

await loadSchema(
`
model M1 {
id String @id
m2_id String @unique
m2 M2 @relation(fields: [m2_id], references: [id])
}
model M2 {
id String @id
m1 M1?
}
`
);

await loadSchema(
`
model M1 {
m2_id String
m2 M2 @relation(fields: [m2_id], references: [id])
@@id([m2_id])
}
model M2 {
id String @id
m1 M1?
}
`
);

await loadSchema(
`
model M1 {
m2_id String
m2 M2 @relation(fields: [m2_id], references: [id])
@@unique([m2_id])
}
model M2 {
id String @id
m1 M1?
}
`
);
});
});

0 comments on commit 9a18af6

Please sign in to comment.