Skip to content

Commit 317ba8d

Browse files
authored
fix: Support implicit many-to-many (#286)
1 parent 6f7cb0e commit 317ba8d

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

packages/schema/src/language-server/validator/datamodel-validator.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,18 @@ export default class DataModelValidator implements AstValidator<DataModel> {
256256
relationOwner = field;
257257
}
258258
} else {
259-
[field, oppositeField].forEach((f) => {
260-
if (!this.isSelfRelation(f, thisRelation.name)) {
261-
accept(
262-
'error',
263-
'Field for one side of relation must carry @relation attribute with both "fields" and "references" fields',
264-
{ node: f }
265-
);
266-
}
267-
});
259+
// if both the field is array, then it's an implicit many-to-many relation
260+
if (!(field.type.array && oppositeField.type.array)) {
261+
[field, oppositeField].forEach((f) => {
262+
if (!this.isSelfRelation(f, thisRelation.name)) {
263+
accept(
264+
'error',
265+
'Field for one side of relation must carry @relation attribute with both "fields" and "references" fields',
266+
{ node: f }
267+
);
268+
}
269+
});
270+
}
268271
return;
269272
}
270273

packages/schema/tests/schema/validation/datamodel-validation.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,23 @@ describe('Data Model Validation Tests', () => {
257257
}
258258
`);
259259

260+
// many-to-many implicit
261+
//https://www.prisma.io/docs/concepts/components/prisma-schema/relations/many-to-many-relations#implicit-many-to-many-relations
262+
await loadModel(`
263+
${prelude}
264+
model Post {
265+
id Int @id @default(autoincrement())
266+
title String
267+
categories Category[]
268+
}
269+
270+
model Category {
271+
id Int @id @default(autoincrement())
272+
name String
273+
posts Post[]
274+
}
275+
`);
276+
260277
// one-to-one incomplete
261278
expect(
262279
await loadModelWithError(`

0 commit comments

Comments
 (0)