Skip to content

Commit

Permalink
fix: foreign key constraint ambiguity in generated delegate prisma sc…
Browse files Browse the repository at this point in the history
…hema (#1060)
  • Loading branch information
ymc9 authored Feb 27, 2024
1 parent 6edfd66 commit ca2a314
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ export class PrismaSchemaGenerator {
new PrismaFieldAttribute('@relation', [
new PrismaAttributeArg('fields', args),
new PrismaAttributeArg('references', args),
// generate a `map` argument for foreign key constraint disambiguation
new PrismaAttributeArg(
'map',
new PrismaAttributeArgValue('String', `${relationField.name}_fk`)
),
])
);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Regression tests', () => {
it('FK Constraint Ambiguity', async () => {
const schema = `
model User {
id String @id @default(cuid())
name String
userRankings UserRanking[]
userFavorites UserFavorite[]
}
model Entity {
id String @id @default(cuid())
name String
type String
userRankings UserRanking[]
userFavorites UserFavorite[]
@@delegate(type)
}
model Person extends Entity {
}
model Studio extends Entity {
}
model UserRanking {
id String @id @default(cuid())
rank Int
entityId String
entity Entity @relation(fields: [entityId], references: [id], onUpdate: NoAction)
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: NoAction)
}
model UserFavorite {
id String @id @default(cuid())
entityId String
entity Entity @relation(fields: [entityId], references: [id], onUpdate: NoAction)
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: NoAction)
}
`;

await loadSchema(schema, { pushDb: false, provider: 'postgresql' });
});
});

0 comments on commit ca2a314

Please sign in to comment.