Skip to content

Commit 23a9bbb

Browse files
authored
fix: Validation error when @@unique attribute is defined in the different model of the relation field (#1430)
1 parent 6a71742 commit 23a9bbb

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ export default class DataModelValidator implements AstValidator<DataModel> {
361361
const containingModel = field.$container as DataModel;
362362
const uniqueFieldList = getUniqueFields(containingModel);
363363

364+
// field is defined in the abstract base model
365+
if (containingModel !== contextModel) {
366+
uniqueFieldList.push(...getUniqueFields(contextModel));
367+
}
368+
364369
thisRelation.fields?.forEach((ref) => {
365370
const refField = ref.target.ref as DataModelField;
366371
if (refField) {
@@ -372,7 +377,7 @@ export default class DataModelValidator implements AstValidator<DataModel> {
372377
}
373378
accept(
374379
'error',
375-
`Field "${refField.name}" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute`,
380+
`Field "${refField.name}" on model "${containingModel.name}" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute`,
376381
{ node: refField }
377382
);
378383
}

packages/schema/tests/schema/validation/datamodel-validation.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ describe('Data Model Validation Tests', () => {
521521
`)
522522
).toMatchObject(
523523
errorLike(
524-
`Field "aId" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute`
524+
`Field "aId" on model "B" is part of a one-to-one relation and must be marked as @unique or be part of a model-level @@unique attribute`
525525
)
526526
);
527527

@@ -736,6 +736,26 @@ describe('Data Model Validation Tests', () => {
736736
).toMatchObject(
737737
errorLike(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`)
738738
);
739+
740+
// one-to-one relation field and @@unique defined in different models
741+
await loadModel(`
742+
${prelude}
743+
744+
abstract model Base {
745+
id String @id
746+
a A @relation(fields: [aId], references: [id])
747+
aId String
748+
}
749+
750+
model A {
751+
id String @id
752+
b B?
753+
}
754+
755+
model B extends Base{
756+
@@unique([aId])
757+
}
758+
`);
739759
});
740760

741761
it('delegate base type', async () => {

0 commit comments

Comments
 (0)