Skip to content

Conversation

@danolekh
Copy link

Summary

Closes #4138

This PR implements enum value support for PostgreSQL integer columns, following the same pattern as existing text and varchar enum support.

import { pgTable, integer, serial } from 'drizzle-orm/pg-core';
const ratingsTable = pgTable('ratings', {
  id: serial('id').primaryKey(),
  rating: integer('rating', { enum: [1, 2, 3, 4, 5] as const }),
});

await db.insert(ratingsTable).values({
  id: 1,
  rating: 5,  // ✅
});

await db.insert(ratingsTable).values({
  id: 2,
  rating: 6,  // ❌ Type '6' is not assignable to type '1 | 2 | 3 | 4 | 5'
});

…g types

Previously, MakeColumnConfig only supported string enum values through
the constraint [string, ...string[]]. This change generalizes it to
[any, ...any[]] to enable enum support for other types like numbers.

This is a foundational change that maintains backward compatibility
while enabling the type system to work with numeric enums in integer
columns.
Add generic TRuntimeConfig parameter to PgIntColumnBaseBuilder to allow
derived classes to extend the runtime configuration. This enables integer
column types to store additional configuration like enum values while
maintaining the base generatedIdentity config.

This change prepares the foundation for adding enum support to integer
columns without breaking existing functionality.
Introduce ColumnDataTypeToEnumValues helper type that maps column data
types to their appropriate enum value types (string[] for string columns,
number[] for number columns).

Update ColumnBaseConfig to use this type-safe enum values constraint
instead of hardcoding string[], enabling proper type inference for
numeric enum values in integer columns.
Implement enum value constraints for integer columns, following the same
pattern as text and varchar columns. Key changes:

- Add generic TEnum parameter to PgIntegerBuilder and PgInteger classes
- Introduce PgIntegerConfig interface with optional enum property
- Update integer() function with overloads supporting enum configuration
- Store and expose enum values through the enumValues property
- Support both 'as const' and regular array syntax for type inference

Example usage:
  integer('rating', { enum: [1, 2, 3, 4, 5] as const })

This enables type-safe integer enums with proper TypeScript inference
for insert and select operations.
Add comprehensive type tests to verify integer enum functionality:

- Test type inference for columns with enum values (1 | 2 | 3 | 4 | 5)
- Test type inference for columns without enums (number)
- Verify enumValues property types for both enum and non-enum columns
- Compare behavior with existing varchar enum support

These tests ensure the type system correctly narrows integer types to
their specific enum values and that the enumValues property reflects
the correct type signature.
Update drizzle-typebox, drizzle-valibot, and drizzle-seed to handle
numeric enum values alongside string enums:

- Modify mapEnumValues() signature to accept string[] | number[]
- Update Valibot type constraints for number enum support
- Update drizzle-seed Column type to accept number[] for enumValues
- Fix drizzle-seed package.json entry points (main/module fields)

These changes ensure the entire ecosystem works correctly with the new
integer enum feature.
@danolekh danolekh marked this pull request as draft December 22, 2025 20:19
@danolekh danolekh marked this pull request as ready for review December 23, 2025 10:28
@danolekh danolekh changed the title [Pg] Add enum support for integer columns [Pg, Sqlite] Add enum support for integer columns Dec 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant