Skip to content

fix(sqlserver): move complex indexes out of the way of ALTER COLUMN (#84) - #85

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/84-sqlserver-alter-column
Oct 20, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/84-sqlserver-alter-column

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #84

Problem

Changing the length, type or nullability of a column a complex index reads, on SQL Server, scaffolded a bare ALTER TABLE [members] ALTER COLUMN [Email_Value] nvarchar(100) NOT NULL;, which SQL Server refuses: The index 'ux_members_email' is dependent on column 'Email_Value' (Msg 5074). EF drops and re-creates its own indexes around the statement; it does not know the complex index exists. Deployment stopped until someone edited the migration.

Change

  • Core: a protected virtual RequiresIndexRecreation(MigrationOperation, IReadOnlySet<string> columns). When it says so for a base operation on the index's table, an unchanged complex index is dropped before the base operations and created after them, the placement every changed index already gets. Off in the core. The columns are the key columns, every array-valued provider option (the INCLUDE list), and the table's columns that the filter or an expression part names.
  • SQL Server: on for AlterColumnOperation on one of those columns.
  • PostgreSQL needs nothing: it rebuilds such indexes itself.

SQLite has the silent form of this problem (a table rebuild drops every complex index) and EF's SQLite generator emits rebuilds after all other operations, so this mechanism cannot fix it; that is tracked separately.

Tests

  • AlterColumnUnderIndexTests: on SQL Server, DropIndex → AlterColumn → CreateIndex for a length change under the index; an ALTER COLUMN on another column leaves the index alone; on PostgreSQL the same change emits no index operations.
  • Live, on Azure SQL Edge (the mssql/server image does not run on this arm64 host): the V1 → V3 migration fails with Msg 5074 on main and applies with this change.
  • verify-the-guard: with the source reverted, the ordering test fails.
  • Full suite 375/375 locally, integration included.

🤖 Generated with Claude Code

)

EF Core drops and re-creates its own indexes around an ALTER COLUMN on
SQL Server; a complex index is not in EF's model, so it stayed, and SQL
Server refused the statement with Msg 5074 ("The index 'ux_members_email'
is dependent on column 'Email_Value'"). Any change of type, length or
nullability of a column a complex index reads blocked deployment until
the migration was edited by hand.

The core differ gains a protected virtual, RequiresIndexRecreation: an
unchanged complex index on a table whose base operations need it gone
is dropped before them and created after them, like a changed one. It
is off in the core; the SQL Server satellite turns it on for ALTER
COLUMN on a column the index reads (key, INCLUDE, or named in the
filter). PostgreSQL rebuilds such indexes itself.

Verified on Azure SQL Edge: without the change the V1 -> V3 migration
fails with Msg 5074, with it it applies. Reverting the source fails the
new ordering test; the PostgreSQL and unrelated-column tests pin that
nothing else moves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working severity/medium Inconsistency or failure in a narrow scenario package/core Affects EFCore.ComplexIndexes package/sqlserver Affects EFCore.ComplexIndexes.SqlServer area/indexes Complex-property, composite, unique and filtered indexes area/migrations The differs, snapshot format, EnsureCreated and design-time wiring labels Oct 19, 2026
Keeps both protected overrides (RequiresIndexRecreation, ImplicitFilter) and both changelog entries.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@CaffeinatedCoder
CaffeinatedCoder merged commit bf26dfe into main Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/84-sqlserver-alter-column branch October 20, 2026 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/indexes Complex-property, composite, unique and filtered indexes area/migrations The differs, snapshot format, EnsureCreated and design-time wiring bug Something isn't working package/core Affects EFCore.ComplexIndexes package/sqlserver Affects EFCore.ComplexIndexes.SqlServer severity/medium Inconsistency or failure in a narrow scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL Server: ALTER COLUMN on a column under a complex index fails at apply (Msg 5074)

1 participant