我有两个用于Django项目的开发机器-塔式和便携式计算机。我使用私有git repo来保持项目同步。我在塔上工作了一段时间,将更改提交到我的git repo(包括数据库)中,然后执行git pull origin master和git reset --hard origin / master,然后在旅行时在笔记本电脑上工作。
我似乎在某个地方犯了一个错误,因为当我如上所述更新笔记本电脑时,我的迁移过程中出现了错误。在塔上,所有迁移都是最新的并且已应用。在笔记本电脑上,我有一些无法应用的迁移。
[X] 0044_remove_document_rotation
[ ] 0041_remove_collectiondocument_position
[ ] 0045_merge_20191023_1922
[X] 0045_auto_20191121_1536
[ ] 0046_merge_20200213_1523
[X] 0046_auto_20200213_1541
[ ] 0047_merge_20200213_1546
这些迁移都在塔上进行了检查。尝试在笔记本电脑上迁移时出现错误:
Applying memorabilia.0041_remove_collectiondocument_position...Traceback (most recent call last):
File "./manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 233, in handle
fake_initial=fake_initial,
File "/home/mark/.virtualenvs/memorabilia-JSON/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 "/home/mark/.virtualenvs/memorabilia-JSON/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 "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/db/migrations/migration.py", line 114, in apply
operation.state_forwards(self.app_label, project_state)
File "/home/mark/.virtualenvs/memorabilia-JSON/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 172, in state_forwards
delay = not old_field.is_relation
AttributeError: 'NoneType' object has no attribute 'is_relation'
该项目同时在塔式计算机和便携式计算机上运行。
我如何将这些迁移应用到笔记本电脑上,或删除引起问题的迁移?
谢谢!
标记
您是否尝试过在笔记本电脑上进行迁移?这会将迁移标记为已应用,因此您不会再遇到麻烦。
制作单个迁移文件:python manage.py migrate --fake <APP_NAME> <MIGRATION>
为应用程序进行所有迁移:python manage.py migrate --fake <APP_NAME>
进行所有迁移:python manage.py migrate --fake
希望这会有所帮助!
问候
[当我遇到这样的同步问题时,作为最后的选择,我经常删除迁移并从头开始。为此:
上面将为您提供一个完全干净的状态。
此后:
python manage.py makemigrations
其次:
python manage.py migrate
如果您的模型设置正确,这将重新创建所有结构而不会出错。最后,检查备份的表,如果没有差异,则替换现有表。如果存在差异,请确保在同步之前进行必要的数据结构修改。
我发现,在处理可管理的数据库大小时,执行此方法通常比找出迁移出问题的链条要快。