fix(postgres): correct SQL for modifying a column to :identity#744
Merged
greg-rychlewski merged 1 commit intoJul 22, 2026
Merged
Conversation
column_change/2 for :modify reused column_type/2 as-is, which for :identity produces "bigint GENERATED BY DEFAULT AS IDENTITY" - valid in a CREATE TABLE/ADD COLUMN column definition, but PostgreSQL rejects that combined with ALTER COLUMN ... TYPE: ERROR 42601 (syntax_error) syntax error at or near "GENERATED" The identity clause must instead be added via a separate ALTER COLUMN ... ADD GENERATED BY DEFAULT AS IDENTITY action. Split the two concerns: modify_column_type/2 emits just the base type for the TYPE clause when modifying to :identity, and the new modify_identity/3 emits the separate ADD GENERATED clause. The sequence-option logic (start_value/increment) is shared via the extracted identity_generated_expr/1, unchanged from before. Fixes #4751
greg-rychlewski
approved these changes
Jul 22, 2026
Member
|
Thanks :) |
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 #4751.
What
column_change/2for{:modify, name, type, opts}reusedcolumn_type/2as-is for theALTER COLUMN ... TYPE ...clause. Fortype == :identity, that function produces"bigint GENERATED BY DEFAULT AS IDENTITY", which is valid inside aCREATE TABLE/ADD COLUMNcolumn definition, but PostgreSQL rejects it combined withALTER COLUMN ... TYPE:The identity clause must instead be added via a separate
ALTER COLUMN ... ADD GENERATED BY DEFAULT AS IDENTITYaction within the sameALTER TABLEstatement.Fix
modify_column_type/2emits just the base type (column_type_name/2) for theTYPEclause when modifying to:identity, leavingcolumn_type/2's behavior forCREATE TABLE/ADD COLUMNuntouched.modify_identity/3emits the separateALTER COLUMN ... ADD GENERATED BY DEFAULT AS IDENTITY(...)clause, appended after the existing null/default/collation clauses.start_value/increment) is extracted intoidentity_generated_expr/1and shared between theCREATE TABLEpath and the newALTERpath, unchanged in behavior.Verification
master).test/ecto/adapters/postgres_test.exscovering{:modify, :field, :identity, []}and withstart_value/incrementoptions, matching the exact SQL PostgreSQL requires.INSERT ... RETURNINGshows the configuredstart_valueis honored. Also confirmed the PostgreSQL requirement that the column beNOT NULLbefore adding an identity, and that this composes correctly with Ecto's existingnull: falsemodify option on a nullable column.This PR was prepared with the assistance of Claude Code (Anthropic's CLI-based coding agent). The root-cause analysis, fix design, and verification steps above were reviewed and confirmed by me before submitting.