Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#22342 closed Bug (worksforme)

Changing a model to an abstract model caused "ValueError: Related model 'app.Model' cannot be resolved"

Reported by: Daniel Hahler Owned by: nobody
Component: Migrations Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have just run into an error with migrate.

I had turned an existing model (which had a database table already) into a abstract base for others.

The generated migration was, and removing it from the migration worked around the problem:

  Applying app.0016_auto_20140326_1723...Traceback (most recent call last):
  File "…/", line 62, in <module>
  File "…/django-master/django/core/management/", line 427, in execute_from_command_line
  File "…/django-master/django/core/management/", line 419, in execute
  File "…/django-master/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "…/django-master/django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File "…/django-master/django/core/management/commands/", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "…/django-master/django/db/migrations/", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "…/django-master/django/db/migrations/", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File "…/django-master/django/db/migrations/", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "…/django-master/django/db/migrations/operations/", line 83, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field_by_name([0])
  File "…/django-master/django/db/backends/", line 443, in remove_field
    if field.db_parameters(connection=self.connection)['type'] is None:
  File "…/django-master/django/db/models/fields/", line 1758, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File "…/django-master/django/db/models/fields/", line 1749, in db_type
    rel_field = self.related_field
  File "…/django-master/django/db/models/fields/", line 1655, in related_field
    return self.foreign_related_fields[0]
  File "…/django-master/django/db/models/fields/", line 1414, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
  File "…/django-master/django/db/models/fields/", line 1401, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "…/django-master/django/db/models/fields/", line 1386, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' %
ValueError: Related model 'app.Model' cannot be resolved

It might be somewhat related to bug #22319.

Change History (2)

comment:1 Changed 4 years ago by Baptiste Mispelon

Resolution: worksforme
Status: newclosed


I can't seem to be able to reproduce this.

I've tried creating a (non-abstract) model, running makemigrations and migrate, then changing the model to have abstract=True and running makemigrations and migrate again.

When I do that, I don't get the error you mention.

Could you provide us with the models you used (or better yet, a simplified version that still reproduces the issue)?


comment:2 Changed 4 years ago by Daniel Hahler

Thanks for looking into this.

I have came across a code comment, where I've linked to this ticket, but the code itself changed quite a lot already.

IIRC your basic test case was what I was doing.

Feel free to close this ticket because of lacking information, and because it has been fixed in the meantime already or was specific to some certain local-only change at that time.

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