fix(#249): Duplicate index for ForeignKeyField added to existing table - #250
Open
jpsca wants to merge 1 commit into
Open
fix(#249): Duplicate index for ForeignKeyField added to existing table#250jpsca wants to merge 1 commit into
jpsca wants to merge 1 commit into
Conversation
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 #249
Summary
When
Router.create(..., auto=...)detects aForeignKeyFieldthat has been added to a pre-existing table, the generated migration contains both:migrator.add_fields(..., fk=pw.ForeignKeyField(...)), which (correctly) creates the FK column and its implicit index, becauseForeignKeyFielddefaults to index=True.migrator.add_index(table, fk_name, unique=False), a redundant call that tries to create the same index a second time.Running the migration on a fresh DB fails with
peewee.OperationalError: index <table>_<fk>_id already existsThe bug only manifests when the FK is added to an existing model. New models generated through
create_modeldon't trigger it (the index is implicit in the CREATE TABLE).Root cause
In
peewee_migrate/auto.py,diff_modelruns the field diff and the index diff independently, with nocoordination:
diff_model_fields(line 154) sees cover infields_to_addand emitsadd_fields(... ForeignKeyField ...).When applied, peewee's standard field-creation path produces the FK column and its index (because
ForeignKeyField.__init__sets index=True by default).diff_model_indexes(line 187) independently comparesmodel._meta.fields_to_index()againstsource._meta.fields_to_index(). The FK's auto-index is in the new set but not the old, so it lands inindexes_to_add(line 204) and emitsadd_index(...)- duplicating what add_fields will already do.Because
diff_model_indexeshas no knowledge of which fields are being newly added in this same migration, it can't tell that those fields' indexes are "free" (created as a side effect of add_fields).cc/ @klen