﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23237	KeyError when upgrading to the new migrations system (related to git and .pyc)	Henrik Heimbuerger	nobody	"There is a bit of an annoying gotcha connected to the upgrade path for the new migrations, git and the .pyc files. I would recommend to at least add this to the upgrade path docs at https://docs.djangoproject.com/en/dev/topics/migrations/#upgrading-from-south.

Here's what happened:
1. I followed the [https://docs.djangoproject.com/en/dev/topics/migrations/#upgrading-from-south upgrade path instructions] pretty much down to the letter: deleted my old South migration files, deleted the .pyc files, ran `makemigrations` and `migrate`. Everything worked fine and life was good.
2. I committed these changes to my git repository. (Of course, I have `*.pyc` in my `.gitignore`.)
3. My coworkers switched to my branch and upgraded to Django 1.7rc2.
4. They ran `manage.py migrate` and got this:

{{{
System check identified no issues (0 silenced).
Unhandled exception in thread started by <function wrapper at 0x03B75630>
Traceback (most recent call last):
  File ""c:\Python27\lib\site-packages\django\utils\autoreload.py"", line 222, in wrapper
    fn(*args, **kwargs)
  File ""c:\Python27\lib\site-packages\django\core\management\commands\runserver.py"", line 106, in inner_run
    self.check_migrations()
  File ""c:\Python27\lib\site-packages\django\core\management\commands\runserver.py"", line 158, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File ""c:\Python27\lib\site-packages\django\db\migrations\executor.py"", line 17, in __init__
    self.loader = MigrationLoader(self.connection)
  File ""c:\Python27\lib\site-packages\django\db\migrations\loader.py"", line 48, in __init__
    self.build_graph()
  File ""c:\Python27\lib\site-packages\django\db\migrations\loader.py"", line 241, in build_graph
    self.graph.add_dependency(key, parent)
  File ""c:\Python27\lib\site-packages\django\db\migrations\graph.py"", line 42, in add_dependency
    raise KeyError(""Dependency references nonexistent parent node %r"" % (parent,))
KeyError: u""Dependency references nonexistent parent node (u'myapp', u'0001_initial')""
}}}

Obviously, the cause is that the `myapp/migrations/0001_initial.pyc` is still there, and while git replaced the `myapp/migrations/0001_initial.py` with the new migration one, the `myapp/migrations/0001_initial.pyc` is still the one from South. The error message is a bit weird, so it took us a few hours to figure out what's going on here.

We had the same issue on three different coworkers' machines, all running some modern Windows (7–8), all running Python 2.7.4+ (32-bit) and SourceTree as the git client.

----

I see three ways this could be tackled:

First of all, a warning about this should probably be added to the upgrade path instructions, to at least save others from spending time to debug this.

Second, the error message could probably be improved. Apparently, when trying to load the `myapp/migrations/0001_initial.pyc`, it detected that this wasn't a proper Django 1.7 migration file (correct) and just silently ignored it (not nice), later leading to that graph dependecy error. But why silently ignore this and not fail fast: ""myapp/migrations/0001_initial.pyc is an invalid migration file!""

Third, there's probably a way for the management command to actively detect stale bytecode caches, but that sounds fairly difficult to implement."	Uncategorized	closed	Migrations	1.7-rc-2	Release blocker	invalid	git, pyc, migrations	cmawebsite@… henrik@…	Unreviewed	0	1	1	1	0	0
