Skip to content

Commit f81e186

Browse files
committed
Fixed pgSchema enum types (fixes #4421)
1 parent 2263c3c commit f81e186

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

changelogs/drizzle-orm/0.43.1.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Fixes
2+
3+
- [Fixed incorrect types of schema enums in PostgreSQL](https://github.com/drizzle-team/drizzle-orm/issues/4421)

drizzle-orm/src/pg-core/schema.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { entityKind, is } from '~/entity.ts';
22
import { SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
3-
import { pgEnumObjectWithSchema, pgEnumWithSchema } from './columns/enum.ts';
3+
import type { NonArray, Writable } from '~/utils.ts';
4+
import { type PgEnum, type PgEnumObject, pgEnumObjectWithSchema, pgEnumWithSchema } from './columns/enum.ts';
45
import { type pgSequence, pgSequenceWithSchema } from './sequence.ts';
56
import { type PgTableFn, pgTableWithSchema } from './table.ts';
67
import { type pgMaterializedView, pgMaterializedViewWithSchema, type pgView, pgViewWithSchema } from './view.ts';
@@ -23,15 +24,25 @@ export class PgSchema<TName extends string = string> implements SQLWrapper {
2324
return pgMaterializedViewWithSchema(name, columns, this.schemaName);
2425
}) as typeof pgMaterializedView;
2526

26-
public enum = ((enumName: any, input: any) => {
27+
public enum<U extends string, T extends Readonly<[U, ...U[]]>>(
28+
enumName: string,
29+
values: T | Writable<T>,
30+
): PgEnum<Writable<T>>;
31+
32+
public enum<E extends Record<string, string>>(
33+
enumName: string,
34+
enumObj: NonArray<E>,
35+
): PgEnumObject<E>;
36+
37+
public enum(enumName: any, input: any): any {
2738
return Array.isArray(input)
2839
? pgEnumWithSchema(
2940
enumName,
3041
[...input] as [string, ...string[]],
3142
this.schemaName,
3243
)
3344
: pgEnumObjectWithSchema(enumName, input, this.schemaName);
34-
}) as any;
45+
}
3546

3647
sequence: typeof pgSequence = ((name, options) => {
3748
return pgSequenceWithSchema(name, options, this.schemaName);

drizzle-orm/type-tests/pg/tables.ts

+34
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,12 @@ await db.refreshMaterializedView(newYorkers2).withNoData().concurrently();
11601160
}
11611161

11621162
{
1163+
const testSchema = pgSchema('test');
1164+
11631165
const e1 = pgEnum('test', ['a', 'b', 'c']);
11641166
const e2 = pgEnum('test', ['a', 'b', 'c'] as const);
1167+
const e3 = testSchema.enum('test', ['a', 'b', 'c']);
1168+
const e4 = testSchema.enum('test', ['a', 'b', 'c'] as const);
11651169

11661170
const test = pgTable('test', {
11671171
col1: char('col1', { enum: ['a', 'b', 'c'] as const }),
@@ -1175,6 +1179,8 @@ await db.refreshMaterializedView(newYorkers2).withNoData().concurrently();
11751179
col9: varchar('col9', { enum: ['a', 'b', 'c'] as const }),
11761180
col10: varchar('col10', { enum: ['a', 'b', 'c'] }),
11771181
col11: varchar('col11'),
1182+
col12: e3('col4'),
1183+
col13: e4('col5'),
11781184
});
11791185

11801186
Expect<Equal<['a', 'b', 'c'], typeof test.col1.enumValues>>;
@@ -1188,11 +1194,17 @@ await db.refreshMaterializedView(newYorkers2).withNoData().concurrently();
11881194
Expect<Equal<['a', 'b', 'c'], typeof test.col9.enumValues>>;
11891195
Expect<Equal<['a', 'b', 'c'], typeof test.col10.enumValues>>;
11901196
Expect<Equal<[string, ...string[]], typeof test.col11.enumValues>>;
1197+
Expect<Equal<['a', 'b', 'c'], typeof test.col12.enumValues>>;
1198+
Expect<Equal<['a', 'b', 'c'], typeof test.col13.enumValues>>;
11911199
}
11921200

11931201
{
1202+
const testSchema = pgSchema('test');
1203+
11941204
const e1 = pgEnum('test', ['a', 'b', 'c']);
11951205
const e2 = pgEnum('test', ['a', 'b', 'c'] as const);
1206+
const e3 = testSchema.enum('test', ['a', 'b', 'c']);
1207+
const e4 = testSchema.enum('test', ['a', 'b', 'c'] as const);
11961208

11971209
const test = pgTable('test', {
11981210
col1: char('col1', { enum: ['a', 'b', 'c'] as const }).generatedAlwaysAs(sql``),
@@ -1206,6 +1218,8 @@ await db.refreshMaterializedView(newYorkers2).withNoData().concurrently();
12061218
col9: varchar('col9', { enum: ['a', 'b', 'c'] as const }).generatedAlwaysAs(sql``),
12071219
col10: varchar('col10', { enum: ['a', 'b', 'c'] }).generatedAlwaysAs(sql``),
12081220
col11: varchar('col11').generatedAlwaysAs(sql``),
1221+
col12: e3('col4').generatedAlwaysAs(sql``),
1222+
col13: e4('col5').generatedAlwaysAs(sql``),
12091223
});
12101224

12111225
Expect<Equal<['a', 'b', 'c'], typeof test.col1.enumValues>>;
@@ -1219,6 +1233,8 @@ await db.refreshMaterializedView(newYorkers2).withNoData().concurrently();
12191233
Expect<Equal<['a', 'b', 'c'], typeof test.col9.enumValues>>;
12201234
Expect<Equal<['a', 'b', 'c'], typeof test.col10.enumValues>>;
12211235
Expect<Equal<[string, ...string[]], typeof test.col11.enumValues>>;
1236+
Expect<Equal<['a', 'b', 'c'], typeof test.col12.enumValues>>;
1237+
Expect<Equal<['a', 'b', 'c'], typeof test.col13.enumValues>>;
12221238
}
12231239

12241240
{
@@ -1439,4 +1455,22 @@ await db.refreshMaterializedView(newYorkers2).withNoData().concurrently();
14391455
const res = await db.select().from(table);
14401456

14411457
Expect<Equal<{ enum: Role | null }[], typeof res>>;
1458+
1459+
const mySchema = pgSchema('my_schema');
1460+
1461+
const schemaRole = mySchema.enum('role', Role);
1462+
1463+
// @ts-expect-error
1464+
mySchema.enum('role', RoleNonString);
1465+
1466+
// @ts-expect-error
1467+
mySchema.enum('role', RolePartiallyString);
1468+
1469+
const schemaTable = mySchema.table('table', {
1470+
enum: schemaRole('enum'),
1471+
});
1472+
1473+
const schemaRes = await db.select().from(schemaTable);
1474+
1475+
Expect<Equal<{ enum: Role | null }[], typeof schemaRes>>;
14421476
}

0 commit comments

Comments
 (0)