Opened 3 years ago

Closed 3 years ago

#22395 closed Bug (fixed)

Deleting a model and fields that relate to it results in ValueError: Related model '<app>.<model>' cannot be resolved

Reported by: Stephen Burrows Owned by: nobody
Component: Migrations Version: 1.7-beta-1
Severity: Normal Keywords:
Cc: Stephen Burrows Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This seems to happen because a single migration is created which both deletes the model and the fields that reference it - but the fields are (sometimes?) deleted second, so the model is already "gone" by the time the migration gets that far.

Workaround is to move the field deletions up in the migrations file to *before* the model deletion.

Generated migration (relevant bits):

class Migration(migrations.Migration):

    operations = [



  File "./", line 10, in <module>
  File ".../django/django/core/management/", line 427, in execute_from_command_line
  File ".../django/django/core/management/", line 419, in execute
  File ".../django/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ".../django/django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File ".../django/django/core/management/commands/", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File ".../django/django/db/migrations/", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File ".../django/django/db/migrations/", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File ".../django/django/db/migrations/", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File ".../django/django/db/migrations/operations/", line 36, in database_forwards
  File ".../django/django/db/backends/sqlite3/", line 117, in add_field
    self._remake_table(model, create_fields=[field])
  File ".../django/django/db/backends/sqlite3/", line 84, in _remake_table
  File ".../django/django/db/backends/", line 191, in create_model
    definition, extra_params = self.column_sql(model, field)
  File ".../django/django/db/backends/", line 108, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File ".../django/django/db/models/fields/", line 1758, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File ".../django/django/db/models/fields/", line 1749, in db_type
    rel_field = self.related_field
  File ".../django/django/db/models/fields/", line 1655, in related_field
    return self.foreign_related_fields[0]
  File ".../django/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/django/db/models/fields/", line 1401, in related_fields
    self._related_fields = self.resolve_related_fields()
  File ".../django/django/db/models/fields/", line 1386, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' %
ValueError: Related model 'brambling.House' cannot be resolved

Change History (8)

comment:1 Changed 3 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 3 years ago by ofirov

I couldn't reproduce the bug. Not on stable (f004374) and not on the 1.7b1 tag. I tried the following:

And tried to create a project with 2 related fields. I made sure that the migration's operations were: First delete the model, then the related fields; But to no avail. The migrations worked fine.

Do you have a link to a full project that reproduced the bug?

Version 0, edited 3 years ago by ofirov (next)

comment:3 Changed 3 years ago by andrewsg

This looks potentially related to the root cause of #22397 and it's possible my work on that bug will affect this one.

comment:4 Changed 3 years ago by Stephen Burrows

I'm technically running 1.7.X, not the 1.7b1 tag. So it could be a newly-introduced bug. I'll test.

The project I'm using is - but we worked around this a while ago.

I'll pull the latest 1.7.X and see if it's been fixed.

Last edited 3 years ago by Stephen Burrows (previous) (diff)

comment:5 Changed 3 years ago by Stephen Burrows

I can't reproduce this exact bug any more in 1.7.X while migrating forward, but it does still show up in reverse migrations.

It does look like this will probably be resolved by #22397.

comment:6 Changed 3 years ago by Tim Graham

@melinath, could you check if this is still an issue as the ticket you referenced above has been resolved? If it still is, some steps to reproduce would be helpful, as it will be easier than reversing them from the details you've provided.

comment:7 Changed 3 years ago by Stephen Burrows

@timo: Yeah, seems fixed. But to find that out I have to checkout a6ecd5dbb34249f756a337c359eef1e8d78dc01e (to avoid #22563) then checkout stable/1.7.x to make the migration. So... yeah, it has been fixed, probably :-p

comment:8 Changed 3 years ago by Tim Graham

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top