Skip to content

fix: Show the correct incomplete error for multiple level inheritance #916

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

Merged
merged 1 commit into from
Jan 1, 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 @@ -235,22 +235,13 @@ export default class DataModelValidator implements AstValidator<DataModel> {
const node = field.$isInherited ? field.$container : field;
const info: DiagnosticInfo<AstNode, string> = { node, code: IssueCodes.MissingOppositeRelation };

let relationFieldDocUri: string;
let relationDataModelName: string;

if (field.$isInherited) {
info.property = 'name';
const container = field.$container as DataModel;
const abstractContainer = container.superTypes.find((x) =>
x.ref?.fields.find((f) => f.name === field.name)
)?.ref as DataModel;

relationFieldDocUri = getDocument(abstractContainer).textDocument.uri;
relationDataModelName = abstractContainer.name;
} else {
relationFieldDocUri = getDocument(field).textDocument.uri;
relationDataModelName = field.$container.name;
}
info.property = 'name';
// use cstNode because the field might be inherited from parent model
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const container = field.$cstNode!.element.$container as DataModel;

const relationFieldDocUri = getDocument(container).textDocument.uri;
const relationDataModelName = container.name;

const data: MissingOppositeRelationData = {
relationFieldName: field.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,5 +633,29 @@ describe('Data Model Validation Tests', () => {
expect(errors.length).toBe(1);

expect(errors[0]).toEqual(`Model A cannot be extended because it's not abstract`);

// relation incomplete from multiple level inheritance
expect(
await loadModelWithError(`
${prelude}
model User {
id Int @id @default(autoincrement())
}

abstract model Base {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int
}

abstract model Base1 extends Base {
isPublic Boolean @default(false)
}

model A extends Base1 {
a String
}
`)
).toContain(`The relation field "user" on model "A" is missing an opposite relation field on model "User"`);
});
});