﻿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
22300	Migrations fail if a relation field is added where a non-rel field was	Stephen Burrows	nobody	"Scenario is this: I have deleted a CharField with choices and replaced it with a ForeignKey to a new table. When Django tries to detect whether this is a ""rename"", it chokes because it *assumes* that any rel field that was added and might have been renamed was previously *also* a rel field.

Traceback:
{{{
Traceback (most recent call last):
  File ""./manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "".../django/django/core/management/__init__.py"", line 427, in execute_from_command_line
    utility.execute()
  File "".../django/django/core/management/__init__.py"", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "".../django/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "".../django/django/core/management/base.py"", line 337, in execute
    output = self.handle(*args, **options)
  File "".../django/django/core/management/commands/makemigrations.py"", line 99, in handle
    changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)
  File "".../django/django/db/migrations/autodetector.py"", line 33, in changes
    changes = self._detect_changes()
  File "".../django/django/db/migrations/autodetector.py"", line 272, in _detect_changes
    old_rel_to = old_field_dec[2]['to']
KeyError: 'to'
}}}

Should be easy fix - just changing this:

{{{
                    if field.rel and field.rel.to:
                        old_rel_to = old_field_dec[2]['to']
}}}

to this:

{{{
                    if field.rel and field.rel.to and 'to' in old_field_dec[2]:
                        old_rel_to = old_field_dec[2]['to']
}}}"	Uncategorized	closed	Migrations	dev	Normal	fixed		Stephen Burrows	Unreviewed	0	0	0	0	0	0
