Skip to content

Keep generatedAlwaysAsIdentity columns in select schema types (typebox/valibot/arktype)#5888

Open
MahinAnowar wants to merge 1 commit into
drizzle-team:mainfrom
MahinAnowar:fix/generated-identity-select-type
Open

Keep generatedAlwaysAsIdentity columns in select schema types (typebox/valibot/arktype)#5888
MahinAnowar wants to merge 1 commit into
drizzle-team:mainfrom
MahinAnowar:fix/generated-identity-select-type

Conversation

@MahinAnowar

Copy link
Copy Markdown

Closes #4724.

createSelectSchema in drizzle-typebox, drizzle-valibot and drizzle-arktype drops columns defined with generatedAlwaysAsIdentity() (and any generated: 'always' column) from the generated type, even though the runtime select schema keeps them. So selecting from a table with an identity primary key gives a schema whose runtime shape and static type disagree.

Cause

The runtime is correct — createSelectSchema uses selectConditions where never: () => false, so generated-always columns are kept on select (only insert/update omit them):

const selectConditions: Conditions = {
	never: () => false,
	// ...
};

But the type-level BuildSchema excludes them for every schema type:

[K in keyof TColumns as ColumnIsGeneratedAlwaysAs<TColumns[K]> extends true ? never : K]

So the column exists at runtime but is missing from the type — the mismatch reported in the issue.

Fix

drizzle-zod already handles this correctly by guarding the exclusion with TType extends 'select':

ColumnIsGeneratedAlwaysAs<TColumns[K]> extends true ? TType extends 'select' ? K : never : K

This applies that exact guard to the typebox, valibot and arktype packages (the issue notes all three share the defect, and they're kept in lockstep with zod). Generated-always columns are now kept for select and still omitted for insert/update, matching the runtime in every package.

Tests

Extends each package's existing table - select type test with a generated: integer().generatedAlwaysAsIdentity() column and asserts it appears in the resulting schema — mirroring the table - select test that already exists in drizzle-zod. Verified each test fails on the current code (Property 'generated' is missing / Type 'false' does not satisfy the constraint 'true' from the Expect<Equal<…>> assertion) and passes with this change. The full runtime suites (vitest run, 62 tests per package) stay green and dprint formatting is clean.

createSelectSchema in drizzle-typebox, drizzle-valibot and drizzle-arktype
dropped columns defined with generatedAlwaysAsIdentity() from the generated
type, even though the runtime select schema keeps them — the type/runtime
mismatch reported in drizzle-team#4724.

BuildSchema's key remapping excluded generated-always columns for every
schema type. drizzle-zod already guards this with `TType extends 'select'`;
apply the same guard to typebox, valibot and arktype so generated columns
are kept for select and still omitted for insert/update.

Extend each package's "table - select" type test with a
generatedAlwaysAsIdentity() column, mirroring the existing drizzle-zod test.
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]: drizzle-typebox ignores generatedAlwaysAsIdentity primary keys.

1 participant