Opened 3 months ago

Closed 3 months ago

#35176 closed New feature (wontfix)

Renaming a field and other attributes result in deleting and recreating a new column.

Reported by: Corentin Bettiol Owned by: nobody
Component: Migrations Version: 4.2
Severity: Normal Keywords: rename, renaming
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi all,
When renaming a field of a model in Django, one would expect the makemigration command to realize it should only rename the column in the db.

However, when also renaming the verbose_name, the makemigration command will create a migration file which will delete the column (and all the data it contains), and create a new column.

See the resulting migrations files generated for an example (new project named "a", new app named "b", a simple model containing 2 fields).

I think the makemigration command should handle this case by not deleting data, because we expect it to know that it only needs to rename the column.

Attachments (3)

0001_initial.py (1.0 KB ) - added by Corentin Bettiol 3 months ago.
First migration: create the model in db, with 2 fields.
0002_rename_a_field_mymodel_a_field_renamed.py (358 bytes ) - added by Corentin Bettiol 3 months ago.
Working rename of a field: field "a_field" was renamed "a_field_renamed", using migrations.RenameField.
0003_remove_mymodel_another_field_and_more.py (604 bytes ) - added by Corentin Bettiol 3 months ago.
Nonworking migration: the field "another_field" was deleted, and a new field named "another_field_renamed" was created. Only difference with the previous migration is that the verbose_name of the field also changed.

Download all attachments as: .zip

Change History (5)

by Corentin Bettiol, 3 months ago

Attachment: 0001_initial.py added

First migration: create the model in db, with 2 fields.

by Corentin Bettiol, 3 months ago

Working rename of a field: field "a_field" was renamed "a_field_renamed", using migrations.RenameField.

by Corentin Bettiol, 3 months ago

Nonworking migration: the field "another_field" was deleted, and a new field named "another_field_renamed" was created. Only difference with the previous migration is that the verbose_name of the field also changed.

comment:1 by Corentin Bettiol, 3 months ago

Summary: Renaming a field and its verbose name results in deleting and recreating a new column in django 4.2Renaming a field and its verbose name result in deleting and recreating a new column in django 4.2

comment:2 by Mariusz Felisiak, 3 months ago

Resolution: wontfix
Status: newclosed
Summary: Renaming a field and its verbose name result in deleting and recreating a new column in django 4.2Renaming a field and other attributes result in deleting and recreating a new column.
Type: BugNew feature

Migrations are unable to detect that field is renamed after any other attribute changes. You need to do this in two steps:

  • rename a field (generate migrations).
  • change a verbose name (generate migrations, this is a no-op).
Note: See TracTickets for help on using tickets.
Back to Top