Skip to content

Refuse an INCLUDE entry that names no column of the table - #147

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/142-include-names-a-column
Oct 20, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/142-include-names-a-column

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #142

Problem

ResolveIncludeList resolves each INCLUDE entry as a property path and falls back to the entry verbatim. With HasComplexIndex(x => x.Email.Value, ix => ix.HasName("ux_email").IsUnique().IncludeProperties("Note")) and then b.Ignore(x => x.Note), both sides of the diff resolved to Note, no index operation was emitted, and the migration was just ALTER TABLE members DROP COLUMN "Note". PostgreSQL dropped the unique index with the column and applied cleanly; the snapshot still listed it, so nothing recreated it. SQL Server failed at apply (Msg 5074).

Change

  • CustomMigrationsModelDiffer.ValidateIncludedColumns: target model only, each entry of the diffed provider's INCLUDE list (Npgsql:IndexInclude, SqlServer:Include) must name a column of the index's table, compared by the provider's IndexNameComparer (ordinal on PostgreSQL, case-insensitive on SQL Server). Otherwise migrations add throws, naming the entry, index and table. Runs after ValidateNoSatelliteOptions, so the core differ on SQL Server/PostgreSQL still reports the missing satellite first, and only the diffed provider's list is checked (a model shared with SQLite carries options SQLite ignores).
  • The verbatim fallback still works for an entry naming a real column. NpgsqlAnnotationForwardingTests' raw_col fixture named no column at all, so it now maps a shadow property to raw_col.
  • The same differ runs in EnsureCreated(), GenerateCreateScript() and Migrate()'s pending-changes check, so those now fail on such a model too. That is documented.
  • ### Changed answers row in the root and the three package changelogs; new docs/upgrading.md entry, INCLUDE entries must name a column; a sentence in docs/sqlserver.md and CLAUDE.md.

Not covered here: a verbatim filter naming a removed column has the same shape. ColumnsReadBy matches the table's column names inside the filter text. Finding a name the filter uses that is not a column would mean parsing SQL identifiers, so that case is noted on #142 rather than added to this PR.

Tests

IncludeColumnValidationTests (12):

  • The issue's model throws on NpgsqlDiff and SqlServerDiff, in both entity-level and property-level form, with no snapshot, and through GenerateCreateScript() with UseNpgsqlComplexIndexes().
  • These still pass: raw_col naming a real column (both providers), NOTE against column note on SQL Server, a snapshot holding the bad entry diffing to a fixed model (both providers), removing the property and the entry together (drop index → drop column → create index), and SQLite ignoring both satellites' INCLUDE lists.

Counterfactuals (verify-the-guard):

  • With the ValidateIncludedColumns call removed, 4 tests fail with "Expected exception … but no exception was thrown": Npgsql entity-level, Npgsql property-level, SQL Server, and no-snapshot. The GenerateCreateScript test was added after this run.
  • With the provider scoping removed, only the SQLite shared-model test fails.
  • With the fix restored, the unit suite (TestCategory!=Integration&TestCategory!=SqlServer) passes 581/581. I didn't run the integration suites locally; their INCLUDE entries are all real property paths.

The snapshot, raw-column and SQLite tests guard against an over-strict check. They pass either way by design.

🤖 Generated with Claude Code

ResolveIncludeList passes an entry that names no property through verbatim
as a column name. When the property an entry named was removed, both sides
of the diff resolved to the same text, no index operation was emitted, and
the migration was a bare DROP COLUMN: PostgreSQL dropped the unique index
with the column and applied cleanly, SQL Server failed with Msg 5074.

In the target model only, each entry of the diffed provider's INCLUDE list
must now name a column of the index's table, compared by the provider's
identifier rule. The verbatim fallback for a real column keeps working, and
a snapshot holding such an entry stays diffable.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred severity/high Wrong results, crash or failed writes in a realistic scenario package/core Affects EFCore.ComplexIndexes package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL 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 20, 2026
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/postgresql Affects EFCore.ComplexIndexes.PostgreSQL package/sqlserver Affects EFCore.ComplexIndexes.SqlServer severity/high Wrong results, crash or failed writes in a realistic scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An INCLUDE entry whose property was removed falls back to verbatim text, and PostgreSQL silently drops the index

1 participant