Fix adding and dropping column with db_column_name in auto migration #983
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.
Fix #945.
Analysis
Adding Column with
db_column_nameWhen adding a new column with
db_column_nameto an existing table, the forward migration succeeds, but a column withnameinstead ofdb_column_nameis created in the actual table.For example, if adding
agefield to theUsersclass and then running the forward migration, the created column name is notuser_agebutage.And more, backward migration after it fails with UndefinedColumnError because
db_column_namein the migration file does not match the column name in the DB.In my analysis, this is because the migration file is created as expected, but migration process create column with
nameinstead ofdb_column_name.This issue would be fixed by deleting the following line.
https://github.com/piccolo-orm/piccolo/blob/master/piccolo/query/methods/alter.py#L320
In my understanding, there is no problem to delete the line beause if
db_column_nameis None, the property returnsname.Dropping Column with
db_column_nameWhen dropping a column from an existing table, the forward migration fails.
For example, if removing
agefield from theUsersclass and then running the forward migration, it fails with UndefinedColumnError.In my analysis, this is because the migration file is created as expected, but migration process attempts to drop the column by specifing
nameinstead ofdb_column_name.This issue would be fixed by changing the argument of the following line to
column.db_column_nameorcolumn.https://github.com/piccolo-orm/piccolo/blob/master/piccolo/apps/migrations/auto/migration_manager.py#L670