Repository navigation
Support indexes on JSON members - #3974
Merged
Merged
Conversation
Preserve JSON index metadata and render member expressions using the query SQL generator, while retaining whole-document index behavior. Detect expression type changes and reject unsupported collection traversal and tsvector combinations. Cover SQL generation, PostgreSQL enforcement, migration model differences, snapshot round-trips, and exact failures for the pinned EF RC2 limitations. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 2918b62e-4cca-481c-876b-722b5ac7cb13
roji
enabled auto-merge (squash)
October 16, 2026 09:57
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Temporal casts can prevent index creation, and nested keys named NULL can silently bypass uniqueness enforcement.
2 open findings
What changed in this PR
Adds PostgreSQL expression indexes for JSON members through HasIndex, addressing indexes that previously targeted the entire document.
Changes:
- Preserves JSON paths and renders member extraction expressions with existing index options.
- Validates unsupported traversal and tracks expression-type changes for migrations.
- Adds SQL, uniqueness, migration, snapshot, and compatibility coverage, including documented RC2 limitations.
| File | Description |
|---|---|
test/EFCore.PG.Tests/Migrations/JsonIndexTest.cs |
Tests scaffolding, snapshots, model differences, and validation. |
test/EFCore.PG.FunctionalTests/Migrations/NpgsqlMigrationsSqlGeneratorTest.cs |
Tests generated SQL and PostgreSQL behavior. |
src/EFCore.PG/Properties/NpgsqlStrings.resx |
Adds JSON-index diagnostics. |
src/EFCore.PG/Properties/NpgsqlStrings.Designer.cs |
Exposes diagnostic resource accessors. |
src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs |
Renders JSON member index expressions. |
src/EFCore.PG/Metadata/Internal/NpgsqlAnnotationProvider.cs |
Preserves JSON-index metadata and expression types. |
src/EFCore.PG/Metadata/Internal/NpgsqlAnnotationNames.cs |
Defines the expression-type annotation. |
src/EFCore.PG/Infrastructure/Internal/NpgsqlModelValidator.cs |
Rejects unsupported JSON-index configurations. |
src/EFCore.PG/Extensions/BuilderExtensions/NpgsqlIndexBuilderExtensions.cs |
Documents index-method behavior for JSON members. |
Files not reviewed (1)
- src/EFCore.PG/Properties/NpgsqlStrings.Designer.cs: Generated file
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+2491
to
+2494
| var typeMapping = element.StoreTypeMapping ?? element.ContainingColumn.StoreTypeMapping!; | ||
| var expression = new JsonScalarExpression( | ||
| new SqlFragmentExpression(DelimitIdentifier(element.ContainingColumn.Name)), | ||
| path, typeMapping.ClrType, typeMapping, element.IsNullable); |
| new SqlFragmentExpression(DelimitIdentifier(element.ContainingColumn.Name)), | ||
| path, typeMapping.ClrType, typeMapping, element.IsNullable); | ||
|
|
||
| return _querySqlGeneratorFactory.Create().GetCommand(expression).CommandText; |
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.

JSON member selectors currently produce indexes on the containing document rather than the selected value, so unique indexes do not enforce member uniqueness. This adds PostgreSQL expression-index support through the existing
HasIndexAPI.Temporary EF RC2 limitations: dependencies remain unchanged. The pinned build cannot scaffold migration C# that creates member indexes or construct mixed-direction composite member indexes. Active tests assert the exact failures and link to the merged fix in dotnet/efcore#39089; consuming it will require converting those assertions to positive coverage. Uniform-direction composites work.
Previously applied incorrect indexes require explicit drop/recreate migrations; upgrading alone cannot repair them. User documentation changes are prepared separately in
npgsql/docfor the JSON mapping page and 11.0 release notes.Closes #2568
Fixes #3918