Monday, October 24, 2016

Re: Django 1.8: Existing Migrations Files Erroring After FK Model Removed

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?

On Monday, October 24, 2016 at 3:22:18 PM UTC-4, dy...@dylan-jenkinson.nz wrote:
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'])


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/310238d8-dd59-419e-8760-e6477f5e10c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment