Skip to content

[Pg-kit]: Fix invalid nextval default rendering in pg introspection#5416

Open
jimyungkoh wants to merge 1 commit intodrizzle-team:mainfrom
jimyungkoh:fix/pg-kit-nextval-introspect
Open

[Pg-kit]: Fix invalid nextval default rendering in pg introspection#5416
jimyungkoh wants to merge 1 commit intodrizzle-team:mainfrom
jimyungkoh:fix/pg-kit-nextval-introspect

Conversation

@jimyungkoh
Copy link

Summary

Fixes invalid TypeScript generation for Postgres sequence-based numeric defaults during introspection.

Closes #5413

Root Cause

Default-expression metadata could be missed in schema-sensitive cases due to internals key mismatch and non-schema-aware lookup paths. When missed, nextval(...) could be emitted without sql\`` wrapping.

Changes

  • Normalize internal metadata keys to schema.table in pg serializer.
  • Make default/array internals lookup schema-aware in introspect-pg.
  • Force numeric nextval(...) and pg_catalog.nextval(...) defaults to expression rendering.
  • Use proper view identity (name + schema) in view column mapping path.
  • Add regressions:
    • sequence default on integer (public)
    • sequence default on integer (non-public schema)
    • pg_catalog.nextval(...)
    • duplicate table names across schemas

Validation

  • pnpm --filter drizzle-kit exec vitest tests/introspect/pg.test.ts
    • pass: 38 passed | 1 skipped
  • pnpm --filter drizzle-kit exec vitest tests/introspect
    • environment failures only:
      • sqlite native binding missing (better-sqlite3)
      • docker socket unavailable for docker-dependent suites

The pg introspection flow could emit sequence-based numeric defaults as
`.default(nextval(...))`, which generates invalid TypeScript in pulled schema
files (issue drizzle-team#5413).

This change normalizes pg serializer internals to schema-qualified table keys,
makes introspect default metadata lookup schema-aware, and forces
`nextval(...)` / `pg_catalog.nextval(...)` numeric defaults to render as SQL
expressions.

Also adds regression tests for:
- non-public schema sequence defaults
- pg_catalog.nextval defaults
- duplicate table names across schemas

Validation:
- `pnpm --filter drizzle-kit exec vitest tests/introspect/pg.test.ts` passed
  (`38 passed | 1 skipped`)
- `pnpm --filter drizzle-kit exec vitest tests/introspect` failed only due
  environment issues (missing better-sqlite3 native binding, no docker socket)
@jimyungkoh jimyungkoh force-pushed the fix/pg-kit-nextval-introspect branch from 25cf3af to a52fc21 Compare March 2, 2026 10:41
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.

[BUG]: Invalid schema default value generated for sequence-based primary keys in Postgres

1 participant