#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 |
Description
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']
Change History (5)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
My pull request (https://github.com/django/django/pull/2452) includes a new test that fails without this fix.
comment:3 by , 11 years ago
Cc: | added |
---|
comment:4 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
Hi,
I can't seem to be able to reproduce this.
I used this model:
After that, I ran
makemigrations
which correctly created the migration file.I then changed the models to:
Running
makemigrations
again works and correctly creates a second migration files.Can you show us the models you are using?
Thanks.