Don't pass type: :id to scope references in generated migrations#6635
Open
eagle-head wants to merge 2 commits intophoenixframework:mainfrom
Open
Don't pass type: :id to scope references in generated migrations#6635eagle-head wants to merge 2 commits intophoenixframework:mainfrom
eagle-head wants to merge 2 commits intophoenixframework:mainfrom
Conversation
The scope reference in the migration template was passing type: :id explicitly to references(). In Ecto's MySQL adapter, :id maps to integer (32-bit signed), while the default PK is bigint unsigned, causing ER_CANT_CREATE_TABLE (errno 150). By omitting type: when it's :id, Ecto defaults to :bigserial which correctly maps to BIGINT UNSIGNED on MySQL. Non-:id types like :binary_id are still passed explicitly. Fixes phoenixframework#6422
Verifies that scoped resources with foreign key references work correctly on MySQL after the scope migration template fix.
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.
Summary
Fixes #6422.
The scope reference in the migration template (
migration.exs.eex, line 10) was passingtype: :idexplicitly toreferences(). In Ecto's MySQL adapter,:idmaps tointeger(signed 32-bit), while the default PK isbigint unsigned auto_increment. This type mismatch causes:Ecto type mapping on MySQL
references()option:bigserial(default)BIGINT UNSIGNEDtype: :bigserial:bigserialBIGINT UNSIGNEDtype: :id:idinteger(32-bit)Before (broken on MySQL)
After (works on all adapters)
By omitting
type:when it's:id, Ecto defaults to:bigserialwhich correctly maps toBIGINT UNSIGNEDon MySQL. Non-:idtypes like:binary_idare still passed explicitly.Changes
priv/templates/phx.gen.schema/migration.exs.eex— conditionally omittype:for scope references whenschema_migration_type == :id:id(omitted) and:binary_id(kept) scope types