Skip to content

Commit 3890ebf

Browse files
ymc9claude
andcommitted
fix: auto-add "views" preview feature to generated Prisma schema (#2376)
When a ZModel schema contains `view` declarations without an explicit generator block, the generated Prisma schema now automatically includes `previewFeatures = ["views"]` in the default generator, preventing the P1012 validation error during `db push` and `migrate`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f3a9850 commit 3890ebf

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,23 @@ export class PrismaSchemaGenerator {
176176

177177
private generateDefaultGenerator(prisma: PrismaModel) {
178178
const gen = prisma.addGenerator('client', [{ name: 'provider', text: '"prisma-client-js"' }]);
179+
180+
const previewFeatures: string[] = [];
181+
179182
const dataSource = this.zmodel.declarations.find(isDataSource);
180183
if (dataSource?.fields.some((f) => f.name === 'extensions')) {
181-
// enable "postgresqlExtensions" preview feature
182-
gen.fields.push({ name: 'previewFeatures', text: '["postgresqlExtensions"]' });
184+
previewFeatures.push('postgresqlExtensions');
185+
}
186+
187+
if (this.zmodel.declarations.some((d) => isDataModel(d) && d.isView)) {
188+
previewFeatures.push('views');
189+
}
190+
191+
if (previewFeatures.length > 0) {
192+
gen.fields.push({
193+
name: 'previewFeatures',
194+
text: JSON.stringify(previewFeatures),
195+
});
183196
}
184197
}
185198

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { PrismaSchemaGenerator } from '@zenstackhq/sdk';
2+
import { loadSchema } from '@zenstackhq/testtools';
3+
import { describe, expect, it } from 'vitest';
4+
5+
// https://github.com/zenstackhq/zenstack/issues/2376
6+
describe('Regression for issue 2376', () => {
7+
it('should include views preview feature when schema contains views', async () => {
8+
const model = await loadSchema(`
9+
datasource db {
10+
provider = 'sqlite'
11+
url = 'file:./test.db'
12+
}
13+
14+
model User {
15+
id Int @id @default(autoincrement())
16+
email String @unique
17+
name String?
18+
posts Post[]
19+
}
20+
21+
model Post {
22+
id Int @id @default(autoincrement())
23+
title String
24+
author User? @relation(fields: [authorId], references: [id])
25+
authorId Int?
26+
}
27+
28+
view UserPostCount {
29+
id Int @unique
30+
name String
31+
postCount Int
32+
}
33+
`);
34+
35+
const generator = new PrismaSchemaGenerator(model);
36+
const prismaSchema = await generator.generate();
37+
38+
// The generated Prisma schema should include previewFeatures with "views"
39+
expect(prismaSchema).toContain('previewFeatures');
40+
expect(prismaSchema).toContain('views');
41+
});
42+
});

0 commit comments

Comments
 (0)