Skip to content

Commit 75d77de

Browse files
ymc9claude
andauthored
fix(orm): handle self-referential relations in delegate models during schema push (#2449)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 07586f2 commit 75d77de

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

packages/orm/src/client/helpers/schema-db-pusher.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export class SchemaDbPusher<Schema extends SchemaDef> {
8484
for (const field of Object.values(model.fields)) {
8585
// relation order
8686
if (field.relation && field.relation.fields && field.relation.references) {
87+
// skip self-referential relations to avoid false cycle in toposort
88+
if (field.type === model.name) {
89+
continue;
90+
}
8791
const targetModel = requireModel(this.schema, field.type);
8892
// edge: fk side -> target model
8993
graph.push([model, targetModel]);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
4+
// https://github.com/zenstackhq/zenstack/issues/2435
5+
describe('Regression for issue 2435', () => {
6+
it('should not throw cyclic dependency error when delegate model has a self-referential relation', async () => {
7+
await expect(
8+
createTestClient(
9+
`
10+
enum ContentType {
11+
POST
12+
ARTICLE
13+
QUESTION
14+
}
15+
16+
model Content {
17+
id Int @id @default(autoincrement())
18+
type ContentType
19+
@@delegate(type)
20+
}
21+
22+
model Post extends Content {
23+
post1s Post1[]
24+
replies Post[] @relation("PostReplies")
25+
parentId Int?
26+
parent Post? @relation("PostReplies", fields: [parentId], references: [id])
27+
}
28+
29+
model Post1 extends Content {
30+
post Post @relation(fields: [postId], references: [id])
31+
postId Int
32+
}
33+
`,
34+
),
35+
).toResolveTruthy();
36+
});
37+
});

0 commit comments

Comments
 (0)