Skip to content

Commit ca2a314

Browse files
authored
fix: foreign key constraint ambiguity in generated delegate prisma schema (#1060)
1 parent 6edfd66 commit ca2a314

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

packages/schema/src/plugins/prisma/schema-generator.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ export class PrismaSchemaGenerator {
456456
new PrismaFieldAttribute('@relation', [
457457
new PrismaAttributeArg('fields', args),
458458
new PrismaAttributeArg('references', args),
459+
// generate a `map` argument for foreign key constraint disambiguation
460+
new PrismaAttributeArg(
461+
'map',
462+
new PrismaAttributeArgValue('String', `${relationField.name}_fk`)
463+
),
459464
])
460465
);
461466
} else {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('Regression tests', () => {
4+
it('FK Constraint Ambiguity', async () => {
5+
const schema = `
6+
model User {
7+
id String @id @default(cuid())
8+
name String
9+
10+
userRankings UserRanking[]
11+
userFavorites UserFavorite[]
12+
}
13+
14+
model Entity {
15+
id String @id @default(cuid())
16+
name String
17+
type String
18+
userRankings UserRanking[]
19+
userFavorites UserFavorite[]
20+
21+
@@delegate(type)
22+
}
23+
24+
model Person extends Entity {
25+
}
26+
27+
model Studio extends Entity {
28+
}
29+
30+
31+
model UserRanking {
32+
id String @id @default(cuid())
33+
rank Int
34+
35+
entityId String
36+
entity Entity @relation(fields: [entityId], references: [id], onUpdate: NoAction)
37+
userId String
38+
user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: NoAction)
39+
}
40+
41+
model UserFavorite {
42+
id String @id @default(cuid())
43+
44+
entityId String
45+
entity Entity @relation(fields: [entityId], references: [id], onUpdate: NoAction)
46+
userId String
47+
user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: NoAction)
48+
}
49+
`;
50+
51+
await loadSchema(schema, { pushDb: false, provider: 'postgresql' });
52+
});
53+
});

0 commit comments

Comments
 (0)