﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28350	UnboundLocalError in RenameField.state_forwards() when renaming nonexistent field	Daniel Hahler	Simon Charette	"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."	Bug	closed	Migrations	1.11	Release blocker	fixed			Ready for checkin	1	0	0	0	0	0
