Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#22300 closed Uncategorized (fixed)

Migrations fail if a relation field is added where a non-rel field was

Reported by: Stephen Burrows Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Stephen Burrows Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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 (most recent call last):
  File "./", line 10, in <module>
  File ".../django/django/core/management/", line 427, in execute_from_command_line
  File ".../django/django/core/management/", line 419, in execute
  File ".../django/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ".../django/django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File ".../django/django/core/management/commands/", line 99, in handle
    changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)
  File ".../django/django/db/migrations/", line 33, in changes
    changes = self._detect_changes()
  File ".../django/django/db/migrations/", 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
                        old_rel_to = old_field_dec[2]['to']

to this:

                    if field.rel and and 'to' in old_field_dec[2]:
                        old_rel_to = old_field_dec[2]['to']

Change History (5)

comment:1 Changed 9 years ago by Baptiste Mispelon


I can't seem to be able to reproduce this.

I used this model:

class Foo(models.Model):
    foo = models.CharField(max_length=10)

After that, I ran makemigrations which correctly created the migration file.
I then changed the models to:

class Bar(models.Model):

class Foo(models.Model):
    foo = models.ForeignKey(Bar)

Running makemigrations again works and correctly creates a second migration files.

Can you show us the models you are using?


comment:2 Changed 9 years ago by Stephen Burrows

My pull request ( includes a new test that fails without this fix.

comment:3 Changed 9 years ago by Stephen Burrows

Cc: Stephen Burrows added

comment:4 Changed 9 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 35ed792cf248d6b245e299d13fe47b56f4c6bf42:

Fixed #22300 -- Fixed crash in migrations when changing non-relational field to relational.

comment:5 Changed 9 years ago by Tim Graham <timograham@…>

In b4f165fe941b1aaca49e417330103a02fecb2e85:

[1.7.x] Fixed #22300 -- Fixed crash in migrations when changing non-relational field to relational.

Backport of 35ed792cf2 from master

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