Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23938 closed Bug (fixed)

changing a ManyToManyField to a CharField while also deleting the model being connected to breaks migrations

Reported by: Michael D. Hoyle Owned by: Markus Holtermann
Component: Migrations Version: 1.7
Severity: Release blocker Keywords:
Cc: info+coding@… 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

if you have model.py:

class Thing(models.Model):
  #fields

class ThingList(models.Model):
  things = models.ManyToManyField('Thing')


then run migrations, then change it to:

class ThingList(models.Model):
  things = models.CharField(max_length=200)

(note the removal of the Thing model)
and then make migrations, running the migrations fails with

  Applying brokenmigration.0002_auto_20141130_2034...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 124, in database_forwards
    from_model = from_state.render().get_model(app_label, self.model_name)
  File "/home/hoylemd/django-env/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 89, in render
    model=lookup_model,
ValueError: Lookup failed for model referenced by field brokenmigration.ThingList.things: brokenmigration.Thing

I have made a github repository demonstrating the behavior: https://github.com/hoylemd/djangomigrationsbug

I'm using version 1.7.1

Change History (10)

comment:1 by Markus Holtermann, 10 years ago

Cc: info+coding@… added
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by Markus Holtermann, 10 years ago

Component: UncategorizedMigrations
Severity: NormalRelease blocker

I classify this as a release blocker since it breaks a new feature introduced in 1.7.

comment:3 by Markus Holtermann, 10 years ago

Owner: changed from nobody to Markus Holtermann
Status: newassigned

comment:4 by Markus Holtermann, 10 years ago

Has patch: set
Needs documentation: set
Needs tests: set
Patch needs improvement: set

I attached an initial pull-request to work with: https://github.com/django/django/pull/3665

comment:5 by Markus Holtermann, 10 years ago

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:6 by Tim Graham, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 623ccdd598625591d1a12fc1564cf3ef9a87581f:

Fixed #23938 -- Added migration support for m2m to concrete fields and vice versa

Thanks to Michael D. Hoyle for the report and Tim Graham for the review.

comment:8 by Tim Graham <timograham@…>, 10 years ago

In 1702bc52cc20ed0729893177fc8f4391b4b3183c:

[1.7.x] Fixed #23938 -- Added migration support for m2m to concrete fields and vice versa

Thanks to Michael D. Hoyle for the report and Tim Graham for the review.

Backport of 623ccdd598625591d1a12fc1564cf3ef9a87581f from master

comment:9 by Tim Graham <timograham@…>, 10 years ago

In 79645529e7ce03a7eef1e51e7058a9880f83efb5:

Revert "[1.7.x] Fixed #23938 -- Added migration support for m2m to concrete fields and vice versa"

This reverts commit 1702bc52cc20ed0729893177fc8f4391b4b3183c.

This doesn't work on stable/1.7.x because #23844 wasn't backported and we're
not willing to do so because it's a large change.

comment:10 by Tim Graham <timograham@…>, 10 years ago

In 2d638893651052dd8232911f392376c2cd707799:

Removed release note for refs #23938 as it couldn't be backported to 1.7.

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