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