Opened 4 years ago

Last modified 4 years ago

#24157 new Cleanup/optimization

Prompt to add a new field after renaming

Reported by: Basil Upornikov Owned by: nobody
Component: Migrations Version: 1.7
Severity: Normal Keywords:
Cc: Simon Charette Triage Stage: Someday/Maybe
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have noticed a slightly weird logic of makemigration command after renaming a field in a model.py.

So, I did this:

  1. Rename field

from

descr = models.TextField('description')

to

description = models.TextField()
  1. Invoke
    manage.py makemigrations
    

And migration logic decided that I had added a new field description and asked me to provide default value.

But if to perform renaming this way:

from

descr = models.TextField('description')

to

description = models.TextField('description') # note a passed verbose name argument

then migration logic works well and recognizes renaming.

Change History (2)

comment:1 Changed 4 years ago by Simon Charette

Cc: Simon Charette added

The rename detection works by comparing removed and added fields and searching for deconstructed args and kwargs equality. It has no notion of name similarity.

In your case, since the field name and its verbose_name are changed at the same time the autodetector fails to prompt you for a possible rename.

I'm afraid this cannot be solved easily without some kind of heuristics to detect name similarity and a whitelist of specific args or kwargs allowed to be changed during a rename.

I think we should close this ticket as Wont Fix or document the limitations of the actual implementation. Users are still allowed to manually replace the generated AddField and RemoveField with a RenameField operation.

comment:2 Changed 4 years ago by Simon Charette

Triage Stage: UnreviewedSomeday/Maybe
Type: BugCleanup/optimization
Note: See TracTickets for help on using tickets.
Back to Top