70

When I run the following command

python manage.py migrate

I receive this error from django so can't step forward in my practice:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 63, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 48, in __init__
    self.build_graph()
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 241, in build_graph
    self.graph.add_dependency(migration, key, parent)
  File "/home/nikhil/testWeb-devEnv/local/lib/python2.7/site-packages/django/db/migrations/graph.py", line 42, in add_dependency
    raise KeyError("Migration %s dependencies reference nonexistent parent node %r" % (migration, parent))
KeyError: u"Migration testBolt.0001_initial dependencies reference nonexistent parent node (u'delivery_boy', u'0004_auto_20150221_2011')"

How do I solve this problem?

NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45

15 Answers15

67

Solution - 1

Remove pyc files from your migrations folder.

Solution - 2

Need to remove that reference from testBolt.0001_initial by editing migration file.

Solution - 3

  1. Remove the new changes from the models and run python manage.py migrate --fake

  2. Now again modify your models with new changes

  3. Run python manage.py makemigrations

  4. And then again run python manage.py migrate

kae_screechae
  • 169
  • 12
NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45
  • 6
    If removing pyc files could solve the problem, you should do it as first option. Continue with other proposed solutions if it doesn't help. So I would suggest to reorder solutions here. – smido Nov 10 '16 at 09:42
  • 1
    @smido Hello as per your comment I'm arrange solution. – NIKHIL RANE Nov 10 '16 at 09:53
  • I am not all sure this is the best solution, or the solution intended for this type of problem. I came here because I got the same traceback, but I don't know why you got yours. I got mine because I wanted to change a table name and did so in the OS. According to the docs, there is a method expressly designed for changing table names. https://docs.djangoproject.com/en/1.8/ref/schema-editor/#alter-db-table . Is this the cause of your issue? – Malik A. Rumi Nov 11 '16 at 23:02
  • I had to go further: remove my change, remove all migration files (keep the `__init__.py`). Run `makemigrations` to create the initial one. Run `makemigations --fake`. Add in my changes, run `makemigrations`, and finally `migrate`. – Ehvince Jun 02 '20 at 10:29
  • `makemigration` doesn't accept that argument: `makemigrations: error: unrecognized arguments: --fake` – Original BBQ Sauce Feb 11 '21 at 17:48
15

In my case, I had the .py extension in the dependency module name, like this:

dependencies = [
    ('dashboard', '0003_auto_20181024_0603.py'),
    ('auth', '__latest__'),
    ('contenttypes', '__latest__'),
]

I removed the .py, changing the line to this

    ('dashboard', '0003_auto_20181024_0603'),

and that fixed it.

Christian Long
  • 10,385
  • 6
  • 60
  • 58
  • 1
    This fixed my issue - I had followed a tutorial that just had `'name'` in the dependencies list, rather than `('name', '__latest__')` – HammerN'Songs Mar 27 '23 at 21:48
10

I had the same problem. In my case, because I played with migrations manually, I forgot to create __init__.py inside of migrations folder.

TitanFighter
  • 4,582
  • 3
  • 45
  • 73
9

This works for me In your app migrations folder

  1. Delete all the files pyc in your app folder (except the __init__)
  2. Delete all the files in the migrations (except the __init__ )

  3. python manage.py makemigrations

  4. python manage.py migrate
  5. runserver
Adam
  • 2,726
  • 1
  • 9
  • 22
Lê Hoàng Vũ
  • 127
  • 1
  • 2
  • 4
    WARNING: Don't do this in production, or any environment where you need to keep records in your database table. It's fine if you are just messing around on Localhost. – Michael Romrell Nov 06 '20 at 19:31
  • As long as the comment by @MichaelRomrell isn't the first line of the answer itself, the answer needs to be downvoted, not upvoted. – zerohedge Apr 22 '23 at 20:13
5

I had a similar case, running django in windows in a virtual env. In my case the missing dependency was 0001_initial - which was definitely there in the migration folder.

The 'solution' was to remove the pyc files and do another migrate attempt.

jbos1234
  • 93
  • 1
  • 7
5

Here's how it worked for me:

  1. Deleted all the __pycache__ folders inside every app.
  2. Deleted all the files inside the migration folder except for __init.py__ inside each app folder.
  3. python manage.py makemigrations
  4. python manage.py migrate
  5. python manage.py runserver
Maximouse
  • 4,170
  • 1
  • 14
  • 28
Sachin
  • 185
  • 1
  • 3
  • 10
4

I tried NIKHIL's solutions with no luck. What did work for me was:

  • Removing my virtual environment
  • Deleting the migration __pycache__ folders
  • Deleting old migrations
  • Recreating my virtual environment
  • Running migrations
Programmingjoe
  • 2,169
  • 2
  • 26
  • 46
4
KeyError: u"Migration testBolt.0001_initial dependencies reference nonexistent parent node (u'delivery_boy', u'0004_auto_20150221_2011')"

Remove

testBolt.0001_initial

then run migrate again

HoangYell
  • 4,100
  • 37
  • 31
3

Make sure that you have activated your virtual environment.

Dawn T Cherian
  • 4,968
  • 3
  • 24
  • 35
  • 1
    Its the solution if you get error from django's migration, for example : `... initial dependencies reference nonexistent parent node (u'auth', u'0008_alter_user_username_max_length')` – Pierre.Sassoulas Mar 23 '17 at 10:00
3

This worked for me:

  • Delete environment.
  • Crearte new environment with all dependencies
2

I had moved around my virtual environment folder.so I moved it back where it was, worked for me.

1

I just uninstalled Django and reinstalled it:

pip3 uninstall Django

pip3 install Django

then migrated

William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
0

There could be some migration files remaining in the app when you tried the migrate command. First remove all migrations directories from all the modules. For other cases Nikhil Rane has covered it all.

Vishvajit Pathak
  • 3,351
  • 1
  • 21
  • 16
0

This works for me:

  • Remove the current virtual environment and create your new virtual environment.
  • Run this command in the project directory to remove all migration files -
    find . -type d -name "__pycache__" -exec rm -rf "{}" \;
  • Remove database file .sqlite3
  • Now run the migrations using: python manage.py makemigrations
  • Migrate the migrations: python manage.py migrate

Basically, all the below steps are common and I think all have gone through these steps but if you're missing the first step i.e., to recreate the virtual environment then must give a try to it.

Mayur Gupta
  • 303
  • 2
  • 14
  • `find` command in the answer will produce "No such file or directory" errors for nested folders. To avoid that, replace "\" sign with "+" sign in the end of the command, e.g. `find . -type d -name "__pycache__" -exec rm -rf "{}" +;` – AntonioK Jul 29 '23 at 13:18
-2

Go to folder testBolt -> migrations and remove 0001_initial py and pyc files.

NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45
  • 5
    This does not address a situation where you are removing a deprecated app/module. If you remove the '0001' migration, you will have to remove '0002' as it usually depends on 0001...and so on until you have removed all your migrations... – Aaron C. de Bruyn Aug 29 '15 at 23:29