我使用的是Django 2.x
。
我有一个模型user_setting
,它有Country
模型的外键,该模型是在address
模块下创建的。
现在,我正在使用djanog-countries-plus
添加Country数据,因此已将user_setting
模块更改为指向django-countries-plus
的Country型号。
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)
生成的初始迁移为
# 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',
},
),
]
更改ForeignKey字段的引用后生成的新迁移为
# 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'),
),
]
settings
安装了以下应用程序
'countries_plus',
#####################
# created apps
#####################
'accounts',
但是当我运行migrate时,它会给出错误,
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'countries_plus.Country' cannot be resolved
- 完整日志**
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
0002_auto_20181108_0724
中的迁移失败。
我必须对已经在Heroku
上运行的应用程序进行此更改。
2条答案
按热度按时间1rhkuytd1#
这只是一个猜测,但请尝试将依赖关系添加到迁移中:
kyks70gy2#
对我来说,解决方案是删除表Countries并将其添加到同一迁移文件中。如果这是您的一个选项,它将修复您的问题。也许您可以备份该表,并在修复问题后恢复它。
大概是这样的