Skip to content
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

fix: Validation error when @@unique attribute is defined in the different model of the relation field #1430

Merged
merged 2 commits into from
May 11, 2024
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
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}" 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 }
);
}
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" 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`
)
);

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
Loading