diff --git a/packages/schema/src/language-server/validator/datamodel-validator.ts b/packages/schema/src/language-server/validator/datamodel-validator.ts index 0baf5ace3..b4e17e56c 100644 --- a/packages/schema/src/language-server/validator/datamodel-validator.ts +++ b/packages/schema/src/language-server/validator/datamodel-validator.ts @@ -361,6 +361,11 @@ export default class DataModelValidator implements AstValidator { const containingModel = field.$container as DataModel; const uniqueFieldList = getUniqueFields(containingModel); + // field is defined in the abstract base model + if (containingModel !== contextModel) { + uniqueFieldList.push(...getUniqueFields(contextModel)); + } + thisRelation.fields?.forEach((ref) => { const refField = ref.target.ref as DataModelField; if (refField) { @@ -372,7 +377,7 @@ export default class DataModelValidator implements AstValidator { } accept( 'error', - `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`, + `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`, { node: refField } ); } diff --git a/packages/schema/tests/schema/validation/datamodel-validation.test.ts b/packages/schema/tests/schema/validation/datamodel-validation.test.ts index 0bf12245d..e0778da51 100644 --- a/packages/schema/tests/schema/validation/datamodel-validation.test.ts +++ b/packages/schema/tests/schema/validation/datamodel-validation.test.ts @@ -521,7 +521,7 @@ describe('Data Model Validation Tests', () => { `) ).toMatchObject( errorLike( - `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` + `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` ) ); @@ -736,6 +736,26 @@ describe('Data Model Validation Tests', () => { ).toMatchObject( errorLike(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`) ); + + // one-to-one relation field and @@unique defined in different models + await loadModel(` + ${prelude} + + abstract model Base { + id String @id + a A @relation(fields: [aId], references: [id]) + aId String + } + + model A { + id String @id + b B? + } + + model B extends Base{ + @@unique([aId]) + } + `); }); it('delegate base type', async () => {