[英]Django migrations - django.db.migrations.exceptions.NodeNotFoundError
I get the following error when I run any python manage.py function:运行任何 python manage.py 函数时出现以下错误:
raise NodeNotFoundError(self.error_message, self.key, origin=self.origin) django.db.migrations.exceptions.NodeNotFoundError: Migration auth.0010_user_following dependencies reference nonexistent parent node ('accounts', '0002_auto_20180615_2021')
It happened after I tried to reset my migrations by manually deleting the migration files in the migration folders (except the init files - no other files are left in the migration folders).它发生在我尝试通过手动删除迁移文件夹中的迁移文件来重置我的迁移之后(初始化文件除外 - 迁移文件夹中没有其他文件)。
I have tried dropping the database with python manage.py flush, which also doesn't run.我尝试使用 python manage.py flush 删除数据库,它也没有运行。
Any suggestions?有什么建议么? Thanks!
谢谢!
SOLUTION: After a week of google search I ended up reconstructing the referenced migration files manually using the documentation:https://docs.djangoproject.com/en/2.0/howto/writing-migrations/解决方案:经过一周的谷歌搜索,我最终使用文档手动重建了引用的迁移文件:https ://docs.djangoproject.com/en/2.0/howto/writing-migrations/
After that the manage.py migrate and makemigration functions worked again.之后,manage.py migrate 和 makemigration 函数再次起作用。 Never delete migration files without taking a backup first!
千万不要在没有先备份的情况下删除迁移文件!
Unfortunately, manually deleting migrations doesn't reset them.不幸的是,手动删除迁移不会重置它们。 The database knows which migrations have been run, and the error you're seeing is from Django trying to check whether the state of the models in your models modules matches the state of the migrations that have been run (which is to say, whether or not you need to create migrations to match) and also whether there are migrations that have been created but not run--these cases would create warnings.
数据库知道已经运行了哪些迁移,您看到的错误来自 Django 尝试检查模型模块中模型的状态是否与已运行的迁移状态匹配(也就是说,是否不需要创建迁移来匹配)以及是否存在已创建但未运行的迁移——这些情况会产生警告。 In trying to check these things, it tries to load migrations and can't find any of them.
在尝试检查这些东西时,它会尝试加载迁移并且找不到任何迁移。
If you want to reset your migrations, and just have a single migration per app to go from an empty database to your current schema in a single step, I recommend using the squash migrations command .如果你想重置你的迁移,并且每个应用程序只需一个迁移即可在一个步骤中从一个空数据库到你当前的模式,我建议使用squash 迁移命令。 You'll need to have the migrations files back first, though.
不过,您需要先恢复迁移文件。
Alternatively, if you do want to drop and re-create the database altogether, you'll need to do that outside of the management commands, since those do the above checks when they run.或者,如果您确实想完全删除并重新创建数据库,则需要在管理命令之外执行此操作,因为它们在运行时会执行上述检查。 Then you can have an empty database and run
./manage.py makemigrations
and you'll get initial migrations that represent models as they are.然后,您可以拥有一个空数据库并运行
./manage.py makemigrations
,您将获得代表模型的初始迁移。
SOLUTION: After a week of google search I ended up reconstructing the referenced migration files manually using the documentation:https://docs.djangoproject.com/en/2.0/howto/writing-migrations/解决方案:经过一周的谷歌搜索,我最终使用文档手动重建了引用的迁移文件:https ://docs.djangoproject.com/en/2.0/howto/writing-migrations/
After that the manage.py migrate and makemigration functions worked again.之后,manage.py migrate 和 makemigration 函数再次起作用。 Never delete migration files without taking a backup first!
千万不要在没有先备份的情况下删除迁移文件!
I faced the exact same error, and found the constructing the whole database a little complicated I found a way to simply delete the whole database and reload it , (if its useful for you) using the following commands:我遇到了完全相同的错误,发现构建整个数据库有点复杂我找到了一种方法来简单地删除整个数据库并重新加载它,(如果它对你有用的话)使用以下命令:
Step 1:步骤1:
find .寻找 。 -path " /migrations/ .py" -not -name " init .py" -delete
-path " /migrations/.py " -not -name " init .py" -delete
find .寻找 。 -path " /migrations/ .pyc" -delete
-path " /migrations/.pyc " -delete
Step 2:第2步:
Delete the database (sqlite3 in my case)删除数据库(在我的例子中是 sqlite3)
Step 3: Run第 3 步:运行
python manage.py makemigrations python manage.py makemigrations
python manage.py migrate python manage.py 迁移
They should now run successfully他们现在应该成功运行
If you have manually deleted your migrations folder, here are options to directly get them back instead of manually re-creating them.如果您手动删除了迁移文件夹,这里有一些选项可以直接取回它们,而不是手动重新创建它们。 Can't imagine the pain.
无法想象那种痛苦。
Reset that deletion重置删除
To get them back you can do a git reset on the deleted folder.要取回它们,您可以对已删除的文件夹执行 git reset 。
git checkout -- initial/path/to/migrations
This should recover them if they were being tracked.如果他们被跟踪,这应该可以恢复他们。 This also assumes changes were not yet commited.
这也假设尚未提交更改。 If they were commited, see this answer
如果他们已提交,请参阅此答案
In case the migrations were .gitignored
如果迁移被
.gitignored
If the migrations were not tracked by git, you can result to get a clean working tree where django hadn't indexed the run migrations如果 git 没有跟踪迁移,您可能会得到一个干净的工作树,其中 django 没有索引运行迁移
python manage.py makemigrations && python manage.py migrate
python manage.py makemigrations && python manage.py migrate
The above option will work if the changes were unpushed to the remote.如果更改未推送到遥控器,则上述选项将起作用。 Otherwise, you'll have to checkout a commit made before the deletions.
否则,您必须检查在删除之前所做的提交。
The provided options assume you were using git提供的选项假设您使用的是 git
i am just uninstalled my current Django then re-installed its worked for me我刚刚卸载了我当前的 Django,然后重新安装了它为我工作
pip3 uninstall Django pip3 install Django pip3 卸载 Django pip3 安装 Django
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.