Skip to content

fix: wrong validation error when relation field is marked @id instead of @unique #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?
}
`
);
});
});