Skip to content

fix(sqlite-kit): don't crash introspection on a unique index mixing an expression with a column - #6171

Open
webdevsamran wants to merge 1 commit into
drizzle-team:rc5from
webdevsamran:fix/sqlite-unique-expression-index
Open

fix(sqlite-kit): don't crash introspection on a unique index mixing an expression with a column#6171
webdevsamran wants to merge 1 commit into
drizzle-team:rc5from
webdevsamran:fix/sqlite-unique-expression-index

Conversation

@webdevsamran

Copy link
Copy Markdown

Summary

drizzle-kit pull (and therefore Drizzle Studio) currently crashes with

unexpected unique index 'idx_c' with expression value: CREATE TABLE users (...)

when the database contains a multi-column UNIQUE index whose keys mix an expression with a plain column — for example the case-insensitive-unique-email pattern from the official guide with an extra column:

CREATE TABLE users (id integer PRIMARY KEY, email text NOT NULL, type text NOT NULL);
CREATE UNIQUE INDEX idx_c ON users (lower(email), type);

With such an index present, drizzle-kit pull and Studio fail for the whole database — no table is browsable. A non-unique index with identical keys is handled fine, which makes the crash easy to hit by accident.

Root cause

In fromDatabase, SQLite reports expression index keys from pragma_index_info as cid = -2. The unique-constraint promotion loop treats any multi-column unique index containing such a key as unrecoverable and throws, aborting the entire introspection.

A unique index mixing expressions with columns can never be represented as a drizzle unique constraint (constraints reference plain columns only), but it is already fully introspected as an index (entityType: 'indexes', isUnique: true). So this change skips the constraint promotion instead of throwing.

Changes

  • drizzle-kit/src/dialects/sqlite/introspect.ts: replace the throw with a skip (continue) so introspection completes; the index remains available in the introspected schema.
  • drizzle-kit/tests/sqlite/pull.test.ts: regression test that creates the exact schema above and asserts the index survives with isUnique: true, no constraint is emitted, and nothing throws.

Testing evidence

Reproduced against current rc5 HEAD before the fix using an in-memory SQLite database driven through the same query/run/batch interface the test harness uses:

CRASH: unexpected unique index 'idx_c' with expression value: CREATE TABLE `users`(`id` integer primary key, `email` text not null, `type` text not null)

After the fix, the same script completes successfully:

INTROSPECTION OK
indexes: [{"entityType":"indexes","table":"users","name":"idx_c","isUnique":true,"origin":"manual","where":null,"columns":[{"value":"type","isExpression":false},{"value":null,"isExpression":true}]}]
uniques: []

Note: I could not execute the vitest sqlite suite locally on this Windows machine because better-sqlite3@11.9.1 has no prebuilt binding for Node 24 and no MSVC toolchain is available to compile it (a local environment limitation unrelated to this change). The added regression test follows the existing pull.test.ts pattern exactly and is expected to run green in CI.

Fixes #6159

…n expression with a column

A multi-column UNIQUE index whose keys mix an expression with a plain
column (e.g. CREATE UNIQUE INDEX idx ON users (type, lower(email))) made
fromDatabase throw 'unexpected unique index ... with expression value',
which takes down the whole drizzle-kit pull/Studio session for the database.

Such an index cannot be promoted to a unique constraint (constraints
reference plain columns only), but it is still correctly introspected as
an index. Skip the constraint promotion instead of throwing.

Fixes drizzle-team#6159
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

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