当你在终端上输入如下的命令执行数据迁移的时候:
python manage.py makemigrations
复制代码
它报错了!
/usr/lib/python3/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.26.4) or chardet (3.0.4) doesn't match a supported version!
RequestsDependencyWarning)
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/makemigrations.py", line 87, in handle
loader = MigrationLoader(None, ignore_no_migrations=True)
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/loader.py", line 255, in build_graph
self.graph.validate_consistency()
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/graph.py", line 195, in validate_consistency
[n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/graph.py", line 195, in <listcomp>
[n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)]
File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/graph.py", line 58, in raise_error
raise NodeNotFoundError(self.error_message, self.key, origin=self.origin)
django.db.migrations.exceptions.NodeNotFoundError: Migration comments.0001_initial dependencies reference nonexistent parent node ('tweets', '0002_auto_20210426_0742')
复制代码
面对红红的一堆的错误,怒火中烧,而 Django
又没有提供删除迁移文件的命令 ? ,而手动删除又非常的无趣和浪费时间!
我提供了一个 Python
写的脚本,来删除迁移文件,放到你项目的根目录并执行它吧!
import os
for root, dirs, files in os.walk('.'):
if 'migrations' in dirs:
dir = dirs[dirs.index('migrations')]
for root_j, dirs_j, files_j in os.walk(os.path.join(root, dir)):
for file_k in files_j:
if file_k != '__init__.py':
dst_file = os.path.join(root_j, file_k)
print('>>> ', dst_file)
os.remove(dst_file)
复制代码
整体的思路就是,先通过 os.walk
递归遍历项目根路径下所有的文件夹,找到所有名为 migrations
文件夹,除了 __init__.py
文件,删除里面所有的文件。为什么不能删除 __init__.py
文件呢?因为没有 __init__.py
文件就不是一个 Python
包了,执行迁移命令的时候就不会迁移这个 app
下的 Models
这里在提供一种思路,就是先找到所有的
migrations
文件夹,然后删除它们,在原位置重新创建,记得在文件夹里面创建一个名为__init__.py
的空文件哦!
为什么要选 Python
脚本来做这件事情呢?当然是 Python
的跨平台特性,这样不管是 UNIX
还是 Windows
都可以使用。
当然上面这段代码非常的丑陋 ? ,谁有空可以优化一下后再发我一段吧!
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END