Skip to content

Commit 9a18af6

Browse files
authored
fix: wrong validation error when relation field is marked @id instead of @unique (#395)
1 parent ef7acd7 commit 9a18af6

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

packages/schema/src/language-server/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ export function getIdFields(model: DataModel) {
5151
* Gets lists of unique fields declared at the data model level
5252
*/
5353
export function getUniqueFields(model: DataModel) {
54-
const uniqueAttrs = model.attributes.filter((attr) => attr.decl.ref?.name === '@@unique');
54+
const uniqueAttrs = model.attributes.filter(
55+
(attr) => attr.decl.ref?.name === '@@unique' || attr.decl.ref?.name === '@@id'
56+
);
5557
return uniqueAttrs.map((uniqueAttr) => {
5658
const fieldsArg = uniqueAttr.args.find((a) => a.$resolvedParam?.name === 'fields');
5759
if (!fieldsArg || !isArrayExpr(fieldsArg.value)) {

packages/schema/src/language-server/validator/datamodel-validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export default class DataModelValidator implements AstValidator<DataModel> {
342342
thisRelation.fields?.forEach((ref) => {
343343
const refField = ref.target.ref as DataModelField;
344344
if (refField) {
345-
if (refField.attributes.find((a) => a.decl.ref?.name === '@unique')) {
345+
if (refField.attributes.find((a) => a.decl.ref?.name === '@id' || a.decl.ref?.name === '@unique')) {
346346
return;
347347
}
348348
if (uniqueFieldList.some((list) => list.includes(refField))) {

tests/integration/tests/regression/issues.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,65 @@ describe('GitHub issues regression', () => {
5454
expect(queried.posts[0].zenstack_guard).toBeUndefined();
5555
expect(queried.posts[0].zenstack_transaction).toBeUndefined();
5656
});
57+
58+
it('issue 392', async () => {
59+
await loadSchema(
60+
`
61+
model M1 {
62+
m2_id String @id
63+
m2 M2 @relation(fields: [m2_id], references: [id])
64+
}
65+
66+
model M2 {
67+
id String @id
68+
m1 M1?
69+
}
70+
`
71+
);
72+
73+
await loadSchema(
74+
`
75+
model M1 {
76+
id String @id
77+
m2_id String @unique
78+
m2 M2 @relation(fields: [m2_id], references: [id])
79+
}
80+
81+
model M2 {
82+
id String @id
83+
m1 M1?
84+
}
85+
`
86+
);
87+
88+
await loadSchema(
89+
`
90+
model M1 {
91+
m2_id String
92+
m2 M2 @relation(fields: [m2_id], references: [id])
93+
@@id([m2_id])
94+
}
95+
96+
model M2 {
97+
id String @id
98+
m1 M1?
99+
}
100+
`
101+
);
102+
103+
await loadSchema(
104+
`
105+
model M1 {
106+
m2_id String
107+
m2 M2 @relation(fields: [m2_id], references: [id])
108+
@@unique([m2_id])
109+
}
110+
111+
model M2 {
112+
id String @id
113+
m1 M1?
114+
}
115+
`
116+
);
117+
});
57118
});

0 commit comments

Comments
 (0)