Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/schema/src/plugins/prisma/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
LiteralExpr,
Model,
NumberLiteral,
ReferenceExpr,
StringLiteral,
} from '@zenstackhq/language/ast';
import { getIdFields } from '@zenstackhq/sdk';
Expand Down Expand Up @@ -529,9 +528,15 @@ export class PrismaSchemaGenerator {
if (found) {
// replicate the attribute and replace the field reference with the new FK field
const args: PrismaAttributeArgValue[] = [];
const fieldNames: string[] = [];
for (const arg of fields.items) {
if (isReferenceExpr(arg) && arg.target.ref === origForeignKey) {
if (!isReferenceExpr(arg)) {
throw new PluginError(name, 'Unexpected field reference in @@unique attribute');
}

if (arg.target.ref === origForeignKey) {
// replace
fieldNames.push(addedFkField.name);
args.push(
new PrismaAttributeArgValue(
'FieldReference',
Expand All @@ -540,17 +545,21 @@ export class PrismaSchemaGenerator {
);
} else {
// copy
fieldNames.push(arg.target.$refText);
args.push(
new PrismaAttributeArgValue(
'FieldReference',
new PrismaFieldReference((arg as ReferenceExpr).target.$refText)
new PrismaFieldReference(arg.target.$refText)
)
);
}
}

const constraintName = this.truncate(`${dataModel.name}_${fieldNames.join('_')}_unique`);

model.addAttribute('@@unique', [
new PrismaAttributeArg(undefined, new PrismaAttributeArgValue('Array', args)),
new PrismaAttributeArg('map', new PrismaAttributeArgValue('String', constraintName)),
]);
}
}
Expand Down
65 changes: 65 additions & 0 deletions tests/regression/tests/issue-1992.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('issue 1992', () => {
it('regression', async () => {
await loadSchema(
`
enum MyAppUserType {
Local
Google
Microsoft
}

model MyAppCompany {
id String @id @default(cuid())
name String
users MyAppUser[]

userFolders MyAppUserFolder[]
}

model MyAppUser {
id String @id @default(cuid())
companyId String
type MyAppUserType

@@delegate(type)

company MyAppCompany @relation(fields: [companyId], references: [id])
userFolders MyAppUserFolder[]
}

model MyAppUserLocal extends MyAppUser {
email String
password String
}

model MyAppUserGoogle extends MyAppUser {
googleId String
}

model MyAppUserMicrosoft extends MyAppUser {
microsoftId String
}

model MyAppUserFolder {
id String @id @default(cuid())
companyId String
userId String
path String
name String

@@unique([companyId, userId, name])
@@unique([companyId, userId, path])

company MyAppCompany @relation(fields: [companyId], references: [id])
user MyAppUser @relation(fields: [userId], references: [id])
}
`,
{
provider: 'postgresql',
pushDb: false,
}
);
});
});
Loading