Skip to content

fix: check names against the sequences of identity and serial columns (#115) - #125

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/115-implicit-sequence-names
Oct 20, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/115-implicit-sequence-names

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #115

Problem

PostgreSQL creates a sequence for every identity or serial column and names it <table>_<column>_seq. That sequence is in no model, so the relation-namespace check #82 added (#106) missed it. On an int-keyed table, which Npgsql maps as an identity column,

b.ToTable("bookings");
b.HasComplexIndex(x => x.Code, indexName: "bookings_Id_seq");

scaffolded cleanly and failed at apply with relation "bookings_Id_seq" already exists (42P07).

Change

  • NpgsqlComplexIndexMigrationsModelDiffer.ValidateNamesAcrossKinds adds the implicit sequence of each column to the relation namespace of its table's schema. A column has one when its value generation strategy is IdentityByDefaultColumn, IdentityAlwaysColumn or SerialColumn, or its store type is a serial type (serial, bigserial, smallserial, serial2/4/8).
  • ImplicitSequenceName names it as PostgreSQL's ChooseRelationName does: each identifier first cut to 63 bytes, then the longer of table and column shortened a byte at a time until _…_seq fits in 63 bytes, then clipped back to a whole character (pg_mbcliplen).
  • The message names the sequence, the column and the table, since nothing in the model declares that sequence.
  • A known false positive, documented on ImplicitSequenceName: when the name is already taken at CREATE TABLE, PostgreSQL appends a digit instead of failing. So an index created in an earlier migration than the table it shares a name with would apply, and is still reported. A rename is cheaper than a check that guesses the order the database was built in.
  • Changelog: the existing Index and constraint names are not checked against table, view and sequence names in the schema #82 line under ### Fixed (root and PostgreSQL) now covers the identity/serial sequence. I amended that line instead of adding one, because CONTRIBUTING.md#changelog leaves out fixes to something new in the same release, and one line now describes the whole namespace check. README's index-name paragraph and CLAUDE.md say the same.

Tests

  • NpgsqlNameCollisionTests covers the three ways a column gets a sequence: a complex index against an identity key, an exclusion constraint against UseSerialColumn(), and a temporal constraint against HasColumnType("bigserial"). It also accepts a key with ValueGeneratedNever() and the same name in another schema. Implicit_sequence_name_matches_postgresql covers the naming, truncation included: an over-long table, two long halves, and a two-byte character at the cut.
  • PostgresIntegrationTests.Implicit_sequence_name_matches_the_server runs the same rows against PostgreSQL 18 and reads the sequence name back from pg_class. Index_like_identity_sequence_fails_at_apply shows the differ refusing the model, then applies the migration it would have produced (renamed after the diff) and gets 42P07.
  • Reverts:
    • Disabling the new loop fails all four collision tests: the three unit tests and the integration test, each with "no exception was thrown".
    • Dropping the serial-store-type branch fails only the bigserial test.
    • Dropping SerialColumn from the strategy list fails only the UseSerialColumn() test.
  • Full suite, integration included: 438 passed.

🤖 Generated with Claude Code

@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 20, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working severity/low Cosmetic, message or documentation-level difference package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL area/indexes Complex-property, composite, unique and filtered indexes area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints labels Oct 20, 2026
…#115)

PostgreSQL creates a sequence for every identity or serial column, named
<table>_<column>_seq and cut to 63 bytes by its ChooseRelationName. That
sequence is in no model, so ValidateNamesAcrossKinds missed it: a complex
index named "bookings_Id_seq" on an int-keyed table scaffolded and failed
at apply with 42P07.

The relation namespace now holds those names too. ImplicitSequenceName
follows PostgreSQL's truncation, longer half first and never splitting a
character; PostgresIntegrationTests checks the names against a live server
and applies the collision to show the 42P07.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder
CaffeinatedCoder force-pushed the fix/115-implicit-sequence-names branch from c2008ac to ce3302e Compare October 20, 2026 06:37
@CaffeinatedCoder
CaffeinatedCoder merged commit c1df43f into main Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/115-implicit-sequence-names branch October 20, 2026 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/constraints Temporal (WITHOUT OVERLAPS, PERIOD) and exclusion constraints area/indexes Complex-property, composite, unique and filtered indexes bug Something isn't working package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL severity/low Cosmetic, message or documentation-level difference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Index and constraint names are not checked against implicit identity/serial sequences

1 participant