Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#28350 closed Bug (fixed)

UnboundLocalError in RenameField.state_forwards() when renaming nonexistent field

Reported by: Daniel Hahler Owned by: Simon Charette
Component: Migrations Version: 1.11
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I've just seen the following error:

UnboundLocalError: local variable 'field' referenced before assignment

Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    execute_from_command_line(sys.argv)
  File "…/Vcs/django/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "…/Vcs/django/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "…/Vcs/django/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "…/Vcs/django/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "…/Vcs/django/django/core/management/commands/migrate.py", line 92, in handle
    conflicts = executor.loader.detect_conflicts()
  File "…/Vcs/django/django/db/migrations/executor.py", line 81, in _create_project_state
    migration.mutate_state(state, preserve=False)
  File "…/Vcs/django/django/db/migrations/migration.py", line 92, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "…/Vcs/django/django/db/migrations/operations/fields.py", line 289, in state_forwards
    delay = not field.is_relation
UnboundLocalError: local variable 'field' referenced before assignment

Looking at the code shows that field might not be assigned in case new_name is not found: https://github.com/django/django/blob/a6756195c1a11eee10c16c96fe95d52e22e1d9ba/django/db/migrations/operations/fields.py#L284-L289

I've seen this while manually squashing migrations (messing around with the replaces in the first three new migrations), so this is likely to be constructed / caused by doing something unexpected there, but I think it should still get handled in a better way, i.e. by throwing a proper error with information/context about the RenameField operation.

Change History (5)

comment:1 Changed 3 years ago by Simon Charette

Owner: changed from nobody to Simon Charette
Severity: NormalRelease blocker
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:2 Changed 3 years ago by Simon Charette

Has patch: set

comment:3 Changed 3 years ago by Tim Graham

Summary: UnboundLocalError in RenameField.state_forwardsUnboundLocalError in RenameField.state_forwards() when renaming nonexistent field
Triage Stage: AcceptedReady for checkin

comment:4 Changed 3 years ago by Simon Charette <charette.s@…>

Resolution: fixed
Status: assignedclosed

In 5cbcb36:

Fixed #28350 -- Fixed UnboundLocalError crash in RenameField with nonexistent field.

Thanks Tim for the review.

comment:5 Changed 3 years ago by Simon Charette <charette.s@…>

In c1621d80:

[1.11.x] Fixed #28350 -- Fixed UnboundLocalError crash in RenameField with nonexistent field.

Thanks Tim for the review.

Backport of 5cbcb3683964205ce023157ca181d05a31be2ab5 from master

Note: See TracTickets for help on using tickets.
Back to Top