Skip to content

fix: pass 'indexes' invokeSource for MSSQL filtered index WHERE clause#5596

Open
funsaized wants to merge 1 commit intodrizzle-team:betafrom
funsaized:fix/mssql-filtered-index-where-clause
Open

fix: pass 'indexes' invokeSource for MSSQL filtered index WHERE clause#5596
funsaized wants to merge 1 commit intodrizzle-team:betafrom
funsaized:fix/mssql-filtered-index-where-clause

Conversation

@funsaized
Copy link
Copy Markdown

Summary

Fixes #5593

When defining a filtered unique index with where(isNotNull(...)) for SQL Server, drizzle-kit generates a fully qualified column name (e.g., [schema].[table].[column]) inside the WHERE clause, which SQL Server rejects for filtered indexes.

Root Cause

In drizzle-kit/src/dialects/mssql/drizzle.ts, the where clause for indexes was serialized without passing the 'indexes' invokeSource to dialect.sqlToQuery():

// Before (bug):
let where = index.config.where ? dialect.sqlToQuery(index.config.where).sql : '';

// After (fix):
let where = index.config.where ? dialect.sqlToQuery(index.config.where, 'indexes').sql : '';

Without 'indexes', column references in the WHERE clause get fully qualified as [schema].[table].[column]. The core SQL serializer in drizzle-orm/src/sql/sql.ts already has handling for invokeSource === 'indexes' — when set, it returns just the column name [column], which is valid T-SQL for filtered indexes.

This matches how the Postgres dialect already handles index WHERE clauses (passing 'indexes' to sqlToQuery).

Changes

  • drizzle-kit/src/dialects/mssql/drizzle.ts: Pass 'indexes' as invokeSource when serializing the WHERE clause
  • drizzle-kit/tests/mssql/indexes.test.ts: Update test expectations to match the unqualified column names
  • drizzle-kit/tests/mssql/tables.test.ts: Update test expectations to match the unqualified column names

Before / After

Before (invalid T-SQL):

CREATE UNIQUE INDEX [users_employee_id_idx]
ON [user_schema].[users] ([employee_id])
WHERE ([user_schema].[users].[employee_id] is not null);

After (valid T-SQL):

CREATE UNIQUE INDEX [users_employee_id_idx]
ON [user_schema].[users] ([employee_id])
WHERE ([employee_id] is not null);

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