Opened 16 months ago

Last modified 4 months ago

#26720 new Bug

migration optimizer can produce operations that references removed models under certain circumstances

Reported by: Sergey Fedoseev Owned by:
Component: Migrations Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

class Person(models.Model):
    pass

class Book(models.Model):
    author = models.ForeignKey(Person)
class Person(models.Model):
    pass

class Book(models.Model):
    author = models.ForeignKey(Person)

class Reviewer(models.Model):
    pass

class Review(models.Model):
    book = models.ForeignKey(Book)
class Book(models.Model):
    pass

class Reviewer(models.Model):
    pass

class Review(models.Model):
    reviewer = models.ForeignKey(Reviewer)
    book = models.ForeignKey(Book)
$ python manage.py squashmigrations test_app 0003
$ python manage.py migrate test_app
Applying test_app.0001_squashed_0003_auto_20160607_1120...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/sergey/dev/django/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/sergey/dev/django/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/sergey/dev/django/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/sergey/dev/django/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/sergey/dev/django/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/sergey/dev/django/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/home/sergey/dev/django/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/sergey/dev/django/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/sergey/dev/django/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)
  File "/home/sergey/dev/django/django/db/backends/base/schema.py", line 236, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/home/sergey/dev/django/django/db/backends/base/schema.py", line 135, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/home/sergey/dev/django/django/db/models/fields/related.py", line 2010, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File "/home/sergey/dev/django/django/db/models/fields/related.py", line 2001, in db_type
    rel_field = self.related_field
  File "/home/sergey/dev/django/django/db/models/fields/related.py", line 1904, in related_field
    return self.foreign_related_fields[0]
  File "/home/sergey/dev/django/django/db/models/fields/related.py", line 1638, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "/home/sergey/dev/django/django/db/models/fields/related.py", line 1625, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/home/sergey/dev/django/django/db/models/fields/related.py", line 1610, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model u'test_app.Person' cannot be resolved


Change History (7)

comment:1 Changed 16 months ago by Sergey Fedoseev

Owner: changed from nobody to Sergey Fedoseev
Status: newassigned

comment:2 Changed 16 months ago by Sergey Fedoseev

Has patch: set

comment:3 Changed 16 months ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:4 Changed 12 months ago by Tim Graham

Patch needs improvement: set

comment:5 Changed 8 months ago by Simon Charette

Patch needs improvement: unset
Version: 1.8master

New patch PR.

comment:6 Changed 6 months ago by Sergey Fedoseev

Owner: Sergey Fedoseev deleted
Status: assignednew

comment:7 Changed 4 months ago by Tim Graham

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top