Django 1.8: Existing Migrations Files Erroring After FK Model Removed

40 views
Skip to first unread message

dy...@dylan-jenkinson.nz

unread,
Oct 21, 2016, 7:18:05 AM10/21/16
to Django users
Hi there,

In one of our projects we have removed an old model that was no longer used. It was used as a FK on a couple of other models, so there are old migrations that have the model in them. I am trying to migrate into a fresh new DB to do some new work on the app. The migrations proceed happily until they hit this FK, where they then blow up with a ValueError: Related model 'x' cannot be resolved. I thought to try squishing the migrations to remove the need for that FK migration, but that does not seem to have helped. I suspect that this is related to the fact that the model is in a different application, and maybe that means that the squishing cannot figure out that it is no longer there?

What I would like to know is how other people approach this problem? How would you remove an existing but no longer used model from your system? Is there a recommended way of doing this?

Thanks for any help or advice.

Dylan

Tim Graham

unread,
Oct 21, 2016, 2:03:22 PM10/21/16
to Django users
It might be that you've made a mistake such as importing a model for RunPython rather than using apps.get_model(). Could you share the traceback?

dy...@dylan-jenkinson.nz

unread,
Oct 24, 2016, 3:22:18 PM10/24/16
to Django users
Hi Tim,

Traceback is below.

As a temporary measure to get me going I have commented out the existing migrations that added and removed the migrations. There is an initial migration that adds the FK, a RunPython that checks for the FK to perform some logic, and a migration to remove the FK. The RunPython does not import the model that has the FK. It uses the apps.get_model() method. I have also added the function called by RunPython below, in case that is of any use.

Thanks,
Dylan

Traceback:
Traceback (most recent call last):
  File "/home/dev/Programs/pycharm-2016.1.4/helpers/pycharm/django_manage.py", line 41, in <module>
    run_module(manage_file, None, '__main__', True)
  File "/app/python3/lib/python3.5/runpy.py", line 196, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/app/python3/lib/python3.5/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/app/python3/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/wip/projects/progress/base/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/wip/environments/progress/lib/python3.5/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 236, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 135, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/models/fields/related.py", line 2010, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/models/fields/related.py", line 2001, in db_type
    rel_field = self.related_field
  File "/wip/environments/progress/lib/python3.5/site-packages/django/db/models/fields/related.py", line 1904, in related_field
    return self.foreign_related_fields[0]
  File "/wip/environments/progress/lib/python3.5/site-packages/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 "/wip/environments/progress/lib/python3.5/site-packages/django/db/models/fields/related.py", line 1625, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/wip/environments/progress/lib/python3.5/site-packages/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 'workflow.Event' cannot be resolved

RunPython function:
def load_email_sent_workflow(apps, schema_editor):
    if schema_editor.connection.alias == 'default':
        db_alias = schema_editor.connection.alias
        EmailSent = apps.get_model('common', 'EmailSent')
        for email_sent in EmailSent.objects.using(db_alias).exclude(event__isnull=True):
            if email_sent.event:
                email_sent.workflow = email_sent.event.workflow
                email_sent.save(update_fields=['workflow'])


Tim Graham

unread,
Oct 24, 2016, 4:37:16 PM10/24/16
to Django users
I'm not sure. It looks like the crash happens when creating the model in the migration, not during the RunPython function. Could you create a minimal project that reproduces the crash so I can step through it?
Reply all
Reply to author
Forward
0 new messages