Repository navigation
fix(sqlserver): give unique complex indexes over nullable columns EF's IS NOT NULL filter (#88) - #89
Merged
Conversation
…s IS NOT NULL filter (#88) SQL Server treats NULLs as equal in a unique index, which is why EF Core's SqlServerIndexConvention filters its own unique indexes over nullable columns with IS NOT NULL. A unique complex index got no such filter, so on an optional complex property (all columns nullable) the second row without a value failed with a duplicate key. Reproduced on Azure SQL Edge with the migration's target model, as database update renders it; rendering with model: null hides it, because EF's generator then applies its pre-2.0 legacy filter. The core differ gains a protected virtual, ImplicitFilter, resolved on both sides of the diff like the rest of the descriptor. The SQL Server satellite returns [c] IS NOT NULL AND ... over the nullable key columns of a unique, nonclustered index declared without a filter, gated on the model's rendering version, which goes to 3: a snapshot from an earlier 5.x resolves the old way, so existing indexes are rebuilt once instead of never. The snapshot round-trip test that pinned version 2 now reads the current version. Verified by reverting the source: the filter test and the one-time rebuild test fail. Live on SQL Edge: without the change the second row fails, with it both insert. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #88
Problem
On SQL Server a unique complex index over nullable columns admitted only one row without a value. EF Core's
SqlServerIndexConventiongives its own unique indexes over nullable columnsWHERE [c] IS NOT NULL, because SQL Server treats NULLs as equal in a unique index; complex indexes got nothing. On an optional complex property,HasComplexIndex(x => x.Registration!.Vin, isUnique: true)scaffoldedCREATE UNIQUE INDEX [ux_complex_vin] ON [cars] ([Registration_Vin]);, the migration applied, and the second car without a registration failed withCannot insert duplicate key row … The duplicate key value is (<NULL>)(Azure SQL Edge).Change
ImplicitFilter(entityType, storeObject, columns, isUnique, providerAnnotations), consulted when a declaration has no filter, on both sides of the diff. Null in the core.[c] IS NOT NULL AND …over the nullable key columns of a unique, nonclustered index, EF's rule, gated on rendering version ≥ 3.migrations addrebuilds each affected index once, the mechanism 5.4.0 introduced.SnapshotRoundTripTestspinned the literal2; it now readsCurrentRenderingVersion.Changelogs: a
### Changed answersrow for #88 with its Upgrading note (one rebuild per affected index; the snapshot's rendering version moves to 3 on every provider, with no other index change).Merge note: #69, #71, #73 and #87 add the same
### Changed answersheading to the 5.5.0 entry; whichever lands later needs a trivial changelog rebase.Tests
SqlServerNullableUniqueTests: the filter on single and composite indexes over nullable columns matches EF's native answer; no filter for required columns, a declared filter, or a clustered index; a version-2 snapshot rebuilds exactly the two affected indexes, and the current model diffs clean against itself.Side finding, for #83's test-gap list:
MigrationHarness.SqlServerSqlrenders withmodel: null, which makes EF's generator apply its pre-2.0 legacy filter and hides this class of bug.🤖 Generated with Claude Code