Repository navigation
Don't scaffold a foreign key as one-to-one when its unique index is filtered - #39574
Merged
AndriySvyryd merged 4 commits intoNov 17, 2026
Merged
Conversation
Contributor
|
@ajcvickers Awesome, this has tripped up many EF Core Power Tools users |
Contributor
Author
|
@AndriySvyryd Do you want a breaking change for this? The same database now results in a different EF model. |
…iltered A unique index with a filter only guarantees uniqueness for the rows that satisfy the filter, so a foreign key over the same columns can still be one-to-many. Reverse engineering ignored the filter and generated a reference navigation, which throws at runtime once several dependents exist. A unique index now makes the foreign key one-to-one only if it has no filter or the filter just excludes rows where the foreign key columns are null (the filter that providers generate by convention for unique indexes over nullable columns). That is recognized with a conservative pattern match, not by parsing SQL: anything not recognized is treated as restricting, which results in a collection navigation. SQLite never read index filters, so partial unique indexes were scaffolded as plain unique indexes. The filter text is now read from sqlite_master for indexes that pragma_index_list reports as partial, and index filters are asserted in the SQLite migrations round-trip tests. Fixes dotnet#11298 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ajcvickers
force-pushed
the
fix/11298-filtered-unique-index-fk
branch
from
November 14, 2026 12:01
0093d20 to
02fc8dd
Compare
…the SQLite index filter SQLite allows any character at or above U+0080 in an unquoted identifier, but the scan for the WHERE keyword only treated letters, digits, '_' and '$' as identifier characters. An index named, for example, `where☃` was taken to start the filter at the index name, which produced an invalid HasFilter predicate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… ignoring case A database that distinguishes columns by case (SQL Server with a case-sensitive collation, or SQLite with non-ASCII letters) can have both `PersonId` and `personid`. The null-guard check compared filter names with the foreign key columns ignoring case, so a unique index on one column filtered by the other column's IS NOT NULL was taken as a null guard, and the foreign key was scaffolded as one-to-one even though duplicate values are allowed when the other column is null. Filter names are now resolved against all the columns of the table: an exact match wins, and the case-insensitive fallback is used only when it identifies a single column. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A foreign key column named "NULL" matched the bare identifier in `NULL IS NOT NULL`, so a unique index with that always-false filter was taken as a null guard and the foreign key was scaffolded as one-to-one. A bare NULL is the keyword and is now treated as restricting the rows; a quoted "NULL" is still a column. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The custom SQLite scanner and regex-based filter classifier influence relationship cardinality and generated migrations, warranting human validation of unusual SQL forms and provider compatibility.
No open issues 🎉
3 resolved since last review
Review effort: Lite
AndriySvyryd
approved these changes
Nov 17, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
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.


Fixes #11298
Problem
When reverse engineering, a foreign key was scaffolded as one-to-one whenever a unique index existed over exactly its columns. The index filter was ignored. A filtered unique index only guarantees uniqueness for the rows that satisfy the filter, so the relationship can still be one-to-many. The generated reference navigation then throws as soon as several dependents share a principal, for example with the soft-delete/"only one active row" pattern from the issue:
Fix
RelationalScaffoldingModelFactorynow counts a unique index towards making the foreign key unique only if its filter is absent or is a null guard:IS NOT NULLterms joined byAND, each on one of the foreign key's own columns. That is the filter providers generate by convention for unique indexes over nullable columns (e.g. SQL Server's[col] IS NOT NULL), and rows excluded by it don't take part in the relationship, so optional one-to-one relationships stay one-to-one.This is a conservative pattern match, not SQL parsing, so it doesn't need #10183. Anything it doesn't recognize is treated as restricting rows, which gives a collection navigation. That is always valid for the data, whereas wrongly assuming uniqueness gives a navigation that can throw. Identifiers may be quoted as
[x],"x"or`x`, or bare; column names are matched against the database column names. Both regexes use the usual 1 second timeout, and a timeout is treated as "restricting".SQLite never read index filters, so a partial unique index was scaffolded as a plain unique index (wrong navigation, and
HasFiltermissing from the scaffolded code). It now readspartialfrompragma_index_list, and for those indexes takes the filter from the original statement insqlite_master: the text after the firstWHEREoutside quotes, comments and parentheses, with comments removed. This needs a small scanner but no SQL grammar. As a result,AssertIndexFiltersis now enabled for the SQLite migrations tests, which round-trips filters created by EF.Behavior change
Re-scaffolding a database that has a filtered unique index over a foreign key's columns, other than the null guard above, now produces a collection navigation (
WithMany) instead of a reference navigation (WithOne). The index itself is still scaffolded as unique with its filter. On SQLite, partial indexes now also getHasFilter(...)in the scaffolded code.Not changed
pragma_index_xinforeturns a NULL column name). That predates this change and isn't addressed here; it is tracked in SQLite reverse engineering throws InvalidOperationException for indexes on expressions #39573.Tests
HasFiltertext containing quotes, line breaks and braces.🤖 Generated with Claude Code