Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django migrations - django.db.migrations.exceptions.NodeNotFoundError

I get the following error when I run any python manage.py function:

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.

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/

After that the manage.py migrate and makemigration functions worked again. Never delete migration files without taking a backup first!

like image 799
Jaacob Avatar asked Jun 18 '18 19:06

Jaacob


3 Answers

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/

After that the manage.py migrate and makemigration functions worked again. Never delete migration files without taking a backup first!

like image 137
Jaacob Avatar answered Nov 12 '22 07:11

Jaacob


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. 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. 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.

like image 3
ryanmrubin Avatar answered Nov 12 '22 07:11

ryanmrubin


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:

find . -path "/migrations/.py" -not -name "init.py" -delete

find . -path "/migrations/.pyc" -delete

Step 2:

Delete the database (sqlite3 in my case)

Step 3: Run

python manage.py makemigrations

python manage.py migrate

They should now run successfully

like image 2
Harsheen Rajpal Avatar answered Nov 12 '22 07:11

Harsheen Rajpal