Skip to content

Commit 982a36c

Browse files
authored
fix(orm): enum doesn't properly use default postgres schema (#399)
1 parent aa34372 commit 982a36c

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,24 @@ export class PrismaSchemaGenerator {
402402
this.generateEnumField(_enum, field);
403403
}
404404

405-
for (const attr of decl.attributes.filter((attr) => this.isPrismaAttribute(attr))) {
405+
const allAttributes = decl.attributes.filter((attr) => this.isPrismaAttribute(attr));
406+
for (const attr of allAttributes) {
406407
this.generateContainerAttribute(_enum, attr);
407408
}
408409

410+
if (
411+
this.datasourceHasSchemasSetting(decl.$container) &&
412+
!allAttributes.some((attr) => attr.decl.ref?.name === '@@schema')
413+
) {
414+
// if the datasource declared `schemas` and no @@schema attribute is defined, add a default one
415+
_enum.addAttribute('@@schema', [
416+
new PrismaAttributeArg(
417+
undefined,
418+
new PrismaAttributeArgValue('String', this.getDefaultPostgresSchemaName(decl.$container)),
419+
),
420+
]);
421+
}
422+
409423
// user defined comments pass-through
410424
decl.comments.forEach((c) => _enum.addComment(c));
411425
}

tests/e2e/orm/client-api/pg-custom-schema.test.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ model Foo {
3838
const foundSchema = { create: false, read: false, update: false, delete: false };
3939
const db = await createTestClient(
4040
`
41+
enum Role {
42+
ADMIN
43+
USER
44+
}
45+
4146
model Foo {
4247
id Int @id
4348
name String
49+
role Role
4450
}
4551
`,
4652
{
@@ -57,7 +63,9 @@ model Foo {
5763
},
5864
);
5965

60-
await expect(db.$qb.insertInto('Foo').values({ id: 1, name: 'test' }).execute()).toResolveTruthy();
66+
await expect(
67+
db.$qb.insertInto('Foo').values({ id: 1, name: 'test', role: 'ADMIN' }).execute(),
68+
).toResolveTruthy();
6169
await expect(db.$qb.selectFrom('Foo').selectAll().executeTakeFirst()).toResolveTruthy();
6270
await expect(
6371
db.$qb.updateTable('Foo').set({ name: 'updated' }).where('id', '=', 1).execute(),
@@ -75,17 +83,23 @@ datasource db {
7583
defaultSchema = 'mySchema'
7684
}
7785
86+
enum Role {
87+
ADMIN
88+
USER
89+
}
90+
7891
model Foo {
7992
id Int @id
8093
name String
94+
role Role
8195
}
8296
`,
8397
{
8498
provider: 'postgresql',
8599
},
86100
);
87101

88-
await expect(db.foo.create({ data: { id: 1, name: 'test' } })).rejects.toSatisfy(
102+
await expect(db.foo.create({ data: { id: 1, name: 'test', role: 'ADMIN' } })).rejects.toSatisfy(
89103
(e) => e instanceof ORMError && !!e.dbErrorMessage?.includes('relation "mySchema.Foo" does not exist'),
90104
);
91105

@@ -98,17 +112,23 @@ datasource db {
98112
defaultSchema = 'public'
99113
}
100114
115+
enum Role {
116+
ADMIN
117+
USER
118+
}
119+
101120
model Foo {
102121
id Int @id
103122
name String
123+
role Role
104124
}
105125
`,
106126
{
107127
provider: 'postgresql',
108128
},
109129
);
110130

111-
await expect(db1.foo.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
131+
await expect(db1.foo.create({ data: { id: 1, name: 'test', role: 'ADMIN' } })).toResolveTruthy();
112132
});
113133

114134
it('supports custom schemas', async () => {
@@ -123,15 +143,29 @@ datasource db {
123143
url = '$DB_URL'
124144
}
125145
146+
enum FooRole {
147+
ADMIN
148+
USER
149+
@@schema('mySchema')
150+
}
151+
126152
model Foo {
127153
id Int @id
128154
name String
155+
role FooRole
129156
@@schema('mySchema')
130157
}
131158
159+
enum BarRole {
160+
ADMIN
161+
USER
162+
@@schema('public')
163+
}
164+
132165
model Bar {
133166
id Int @id
134167
name String
168+
role BarRole
135169
@@schema('public')
136170
}
137171
`,
@@ -150,8 +184,8 @@ model Bar {
150184
},
151185
);
152186

153-
await expect(db.foo.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
154-
await expect(db.bar.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
187+
await expect(db.foo.create({ data: { id: 1, name: 'test', role: 'ADMIN' } })).toResolveTruthy();
188+
await expect(db.bar.create({ data: { id: 1, name: 'test', role: 'USER' } })).toResolveTruthy();
155189

156190
expect(fooQueriesVerified).toBe(true);
157191
expect(barQueriesVerified).toBe(true);
@@ -226,9 +260,15 @@ datasource db {
226260
url = '$DB_URL'
227261
}
228262
263+
enum Role {
264+
ADMIN
265+
USER
266+
}
267+
229268
model Foo {
230269
id Int @id
231270
name String
271+
role Role
232272
@@schema('mySchema')
233273
}
234274
@@ -252,7 +292,7 @@ model Bar {
252292
},
253293
);
254294

255-
await expect(db.foo.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
295+
await expect(db.foo.create({ data: { id: 1, name: 'test', role: 'ADMIN' } })).toResolveTruthy();
256296
await expect(db.bar.create({ data: { id: 1, name: 'test' } })).toResolveTruthy();
257297

258298
expect(fooQueriesVerified).toBe(true);

0 commit comments

Comments
 (0)