#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 by , 8 years ago
| Owner: | changed from to | 
|---|---|
| Severity: | Normal → Release blocker | 
| Status: | new → assigned | 
| Triage Stage: | Unreviewed → Accepted | 
comment:2 by , 8 years ago
| Has patch: | set | 
|---|
comment:3 by , 8 years ago
| Summary: | UnboundLocalError in RenameField.state_forwards → UnboundLocalError in RenameField.state_forwards() when renaming nonexistent field | 
|---|---|
| Triage Stage: | Accepted → Ready for checkin | 
PR