Skip to content

Commit

Permalink
fix: Validation error when @@unique attribute is defined in the diffe…
Browse files Browse the repository at this point in the history
…rent model of the relation field
  • Loading branch information
jiashengguo committed May 11, 2024
1 parent 6a71742 commit 3491413
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ export default class DataModelValidator implements AstValidator<DataModel> {
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) {
Expand All @@ -372,7 +377,7 @@ export default class DataModelValidator implements AstValidator<DataModel> {
}
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}" of 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 }
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" of 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`
)
);

Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 3491413

Please sign in to comment.