我将Stores
模型从products
应用转移到新stores
应用的步骤。 products.Product
和pages.Page
都有FK到stores.Store
。
./manage.py startapp stores
Store
的引用(旧的迁移脚本除外,导致它在具有类似错误的makemigrations期间失败)./manage.py makemigrations
./manage.py migrate
,但是在第3个模型Page
和应用程序参考Store
时爆炸了FK。移动的模型:
class Store(models.Model):
class Meta:
db_table = 'products_store'
...
参考文献破损的模型:
from stores.models import Store
...
class Page(TimeStampedModel):
...
store = models.ForeignKey(Store, related_name='pages', null=True)
破碎:
Running migrations:
Applying pages.0007_auto_20150112_0321...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
if self.detect_soft_applied(migration):
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/db/migrations/executor.py", line 140, in detect_soft_applied
apps = project_state.render()
File "/Users/john/.venv/myproject/lib/python2.7/site-packages/django/db/migrations/state.py", line 94, in render
raise ValueError(msg.format(field=operations[0][1], model=lookup_model))
ValueError: Lookup failed for model referenced by field pages.Page.store: stores.Store
OSX 10.10.1上的Python 2.7.6和Django 1.7.2。
更新
在此操作上失败的迁移脚本为pages.0007_auto_20150112_0321
:
operations = [
migrations.AlterField(
model_name='page',
name='store',
field=models.ForeignKey(related_name='pages', to='stores.Store', null=True),
preserve_default=True,
),
结合其他观察:
我可以在没有问题的情况下从shell创建上面的FK,但无论to
设置为什么,它都能正常工作。不确定我应该在这里尝试重现错误。
如果我在迁移中将“to”字段更改为“lkjsdf”,则会给出相同类型的错误。
此外,如果我运行./manage.py migrate stores
(指定应用),那么我得到:
Operations to perform:
Apply all migrations: stores
Running migrations:
No migrations to apply.
The following content types are stale and need to be deleted:
products | store
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
Type 'yes' to continue, or 'no' to cancel:
所以它想删除我对stores
的所有引用,这听起来不太好。
很高兴提供更多信息。谢谢!
答案 0 :(得分:1)
在失败的迁移中有类似的问题检查依赖性
例如:
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //AFHTTPResponseSerializer serializer
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
尝试将商店依赖项添加到迁移并尝试。