Skip to content

[BUG]: MS SQL Filtered Index - Drizzle Kit incorrectly generates fully qualified column name in WHERE clause #5593

@HongXianZheng

Description

@HongXianZheng

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

1.0.0-beta.20

What version of drizzle-kit are you using?

1.0.0-beta.20

Other packages

No response

Describe the Bug

Description

When defining a filtered unique index with where(isNotNull(...)) for SQL Server, drizzle-kit generates invalid T-SQL.

The generated SQL fully qualifies the column inside the WHERE clause, which SQL Server rejects for a filtered index.

Example schema

export const users = userSchema.table("users", {
  id: int().identity().primaryKey(),
  employeeId: nvarchar("employee_id", { length: 255 }),
}, (table) => [
  uniqueIndex("users_employee_id_idx")
    .on(table.employeeId)
    .where(isNotNull(table.employeeId)),
]);

Generated Migration

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

Expected Migration

CREATE UNIQUE INDEX [users_employee_id_idx]
ON [user_schema].[users] ([employee_id])
WHERE ([employee_id] IS NOT NULL);

This results in the migration failing. When running directly in SSMS the error is:
The multi-part identifier "schemaName.tableName.columnName" could not be bound.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions