Skip to content

Commit 081e434

Browse files
authored
fix(zmodel): rejects delegate models with missing opposite relation (#490)
1 parent eb1a0c2 commit 081e434

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

packages/language/src/validators/datamodel-validator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export default class DataModelValidator implements AstValidator<DataModel> {
250250
return;
251251
}
252252

253-
if (this.isFieldInheritedFromDelegateModel(field)) {
253+
if (this.isFieldInheritedFromDelegateModel(field, contextModel)) {
254254
// relation fields inherited from delegate model don't need opposite relation
255255
return;
256256
}
@@ -431,8 +431,8 @@ export default class DataModelValidator implements AstValidator<DataModel> {
431431
}
432432

433433
// checks if the given field is inherited directly or indirectly from a delegate model
434-
private isFieldInheritedFromDelegateModel(field: DataField) {
435-
return isDelegateModel(field.$container);
434+
private isFieldInheritedFromDelegateModel(field: DataField, contextModel: DataModel) {
435+
return field.$container !== contextModel && isDelegateModel(field.$container);
436436
}
437437

438438
private validateInherits(model: DataModel, accept: ValidationAcceptor) {

packages/language/test/delegate.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,54 @@ describe('Delegate Tests', () => {
114114
'can only be applied once',
115115
);
116116
});
117+
118+
it('rejects relation missing the opposite side', async () => {
119+
await loadSchemaWithError(
120+
`
121+
datasource db {
122+
provider = 'sqlite'
123+
url = 'file:./dev.db'
124+
}
125+
126+
model A {
127+
id Int @id @default(autoincrement())
128+
b B @relation(fields: [bId], references: [id])
129+
bId Int
130+
type String
131+
@@delegate(type)
132+
}
133+
134+
model B {
135+
id Int @id @default(autoincrement())
136+
}
137+
`,
138+
'missing an opposite relation',
139+
);
140+
141+
await loadSchema(
142+
`
143+
datasource db {
144+
provider = 'sqlite'
145+
url = 'file:./dev.db'
146+
}
147+
148+
model A {
149+
id Int @id @default(autoincrement())
150+
b B @relation(fields: [bId], references: [id])
151+
bId Int
152+
type String
153+
@@delegate(type)
154+
}
155+
156+
model B {
157+
id Int @id @default(autoincrement())
158+
a A[]
159+
}
160+
161+
model C extends A {
162+
c String
163+
}
164+
`,
165+
);
166+
});
117167
});

0 commit comments

Comments
 (0)