djangodjango-countries

ValueError: Related model cannot be resolved in Django


I'm using Django 2.x.

I have a model user_setting which have foreign Key to Country model which was created under address module.

Now, I'm using djanog-countries-plus to add Country data and thus have changed user_setting module to now point to Country model of django-countries-plus.

from countries_plus.models import Country
from django.contrib.auth.models import User

class UserSetting(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    country = models.ForeignKey(Country, on_delete=models.PROTECT, blank=True, null=True, default=None)
    modified = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)

The initial migration generated was

# 000_initial
class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('address', '0001_initial'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='UserSetting',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('modified', models.DateTimeField(auto_now=True)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('country', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='address.Country')),
                ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'user_setting',
            },
        ),
    ]

and the new migration generated after changing the reference of the ForeignKey field is

# 0002_auto_20181108_0724
class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='usersetting',
            name='country',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='countries_plus.Country'),
        ),
    ]

The settings have installed apps like

'countries_plus',

#####################
# created apps
#####################
'accounts',

But when I run migrate, it gives error as

raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'countries_plus.Country' cannot be resolved

Full log

Running migrations:
  Applying accounts.0002_auto_20181108_0724...Traceback (most recent call last):
  File "src/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 495, in alter_field
    new_db_params = new_field.db_parameters(connection=self.connection)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/related.py", line 966, in db_parameters
    return {"type": self.db_type(connection), "check": self.db_check(connection)}
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/related.py", line 963, in db_type
    return self.target_field.rel_db_type(connection=connection)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/related.py", line 878, in target_field
    return self.foreign_related_fields[0]
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/related.py", line 632, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/related.py", line 619, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/related.py", line 604, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'countries_plus.Country' cannot be resolved

The migration fails in 0002_auto_20181108_0724.

I have to make this change to already running application on Heroku.


Solution

  • This is a little bit a guess, but try to add dependency to migration:

    # 0002_auto_20181108_0724
    class Migration(migrations.Migration):
    
        dependencies = [
            ('accounts', '0001_initial'),
            ('countries_plus', '__latest__'),
        ]
    
        operations = [
            migrations.AlterField(
                model_name='usersetting',
                name='country',
                field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='countries_plus.Country'),
            ),
        ]