Repository navigation
fix: check names against the sequences of identity and serial columns (#115) - #125
Merged
Merged
Conversation
…#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
force-pushed
the
fix/115-implicit-sequence-names
branch
from
October 20, 2026 06:37
c2008ac to
ce3302e
Compare
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 #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,scaffolded cleanly and failed at apply with
relation "bookings_Id_seq" already exists(42P07).Change
NpgsqlComplexIndexMigrationsModelDiffer.ValidateNamesAcrossKindsadds the implicit sequence of each column to the relation namespace of its table's schema. A column has one when its value generation strategy isIdentityByDefaultColumn,IdentityAlwaysColumnorSerialColumn, or its store type is a serial type (serial,bigserial,smallserial,serial2/4/8).ImplicitSequenceNamenames it as PostgreSQL'sChooseRelationNamedoes: each identifier first cut to 63 bytes, then the longer of table and column shortened a byte at a time until_…_seqfits in 63 bytes, then clipped back to a whole character (pg_mbcliplen).ImplicitSequenceName: when the name is already taken atCREATE 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.### 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
NpgsqlNameCollisionTestscovers the three ways a column gets a sequence: a complex index against an identity key, an exclusion constraint againstUseSerialColumn(), and a temporal constraint againstHasColumnType("bigserial"). It also accepts a key withValueGeneratedNever()and the same name in another schema.Implicit_sequence_name_matches_postgresqlcovers 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_serverruns the same rows against PostgreSQL 18 and reads the sequence name back frompg_class.Index_like_identity_sequence_fails_at_applyshows the differ refusing the model, then applies the migration it would have produced (renamed after the diff) and gets 42P07.bigserialtest.SerialColumnfrom the strategy list fails only theUseSerialColumn()test.🤖 Generated with Claude Code