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_name
When adding a new column with
db_column_name
to an existing table, the forward migration succeeds, but a column withname
instead ofdb_column_name
is created in the actual table.For example, if adding
age
field to theUsers
class and then running the forward migration, the created column name is notuser_age
butage
.And more, backward migration after it fails with UndefinedColumnError because
db_column_name
in 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
name
instead 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_name
is None, the property returnsname
.Dropping Column with
db_column_name
When dropping a column from an existing table, the forward migration fails.
For example, if removing
age
field from theUsers
class 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
name
instead ofdb_column_name
.This issue would be fixed by changing the argument of the following line to
column.db_column_name
orcolumn
.https://github.com/piccolo-orm/piccolo/blob/master/piccolo/apps/migrations/auto/migration_manager.py#L670