Skip to content

[Pg-kit] collect enums from table columns when not directly exported#5791

Open
lukpod1 wants to merge 1 commit into
drizzle-team:mainfrom
lukpod1:fix/collect-enums-from-table-columns
Open

[Pg-kit] collect enums from table columns when not directly exported#5791
lukpod1 wants to merge 1 commit into
drizzle-team:mainfrom
lukpod1:fix/collect-enums-from-table-columns

Conversation

@lukpod1
Copy link
Copy Markdown

@lukpod1 lukpod1 commented May 21, 2026

Problem

When enums are defined in a separate file (e.g. enums.ts) and imported into schema.ts only for use in table columns — without being re-exported — prepareFromExports never sees them.

generatePgSnapshot then receives enums = [], so pushing to a clean database emits the CREATE TABLE statements that reference the types but skips the CREATE TYPE statements entirely, producing:

PostgresError: type "my_enum" does not exist

This is a common pattern: splitting schema definitions across multiple files is encouraged in the drizzle docs, and there is no obvious reason a user would know they must re-export every enum that a table column references.

Root cause

prepareFromExports only scans Object.values(exports). Enums that are imported transitively (used in columns but not re-exported) are never present in that object.

Fix

After collecting tables, walk each table's columns via getTableColumns (already imported). If a column carries a .enum property that satisfies isPgEnum (already imported) and the enum hasn't been collected yet, add it to the list.

for (const table of tables) {
  for (const column of Object.values(getTableColumns(table))) {
    const enumCol = column as any;
    if (enumCol.enum !== undefined && isPgEnum(enumCol.enum) && !enums.includes(enumCol.enum)) {
      enums.push(enumCol.enum);
    }
  }
}

No new imports or dependencies required.

Tests

Added drizzle-kit/tests/pg-imports.test.ts with two cases:

  1. Enum used in column but not exported → must be found
  2. Enum both exported and used in column → must not be duplicated

@lukpod1 lukpod1 force-pushed the fix/collect-enums-from-table-columns branch from 0b9ec88 to 9311755 Compare May 21, 2026 04:32
Enums defined in a separate file (e.g. enums.ts) and imported into
schema.ts for use in table columns — but not re-exported — were invisible
to prepareFromExports. generatePgSnapshot received enums=[], so pushing
to a clean database omitted all CREATE TYPE statements and produced
"type does not exist" errors.

After collecting tables, walk each table's columns via getTableColumns
and add any PgEnum found in column.enum that hasn't already been collected.
@lukpod1 lukpod1 force-pushed the fix/collect-enums-from-table-columns branch from 9311755 to 9ef14fa Compare May 21, 2026 04:38
@lukpod1 lukpod1 changed the title fix(drizzle-kit): collect enums from table columns when not directly exported [Pg-kit] collect enums from table columns when not directly exported May 21, 2026
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