3YOURMIND/django-replace-migrations

I get a KeyError when I run manage.py makemigrations --replace-all (...)

CelestialGuru opened this issue · 3 comments

I'm unable to follow the step where I makemigrations --replace-all.

Steps I took

Created a new branch, deleted ALL migrations and committed changes.

Created another branch. Used:

>>> from django.apps import apps
>>> print(" ".join(map(str, sorted({model._meta.app_label for model in apps.get_models()}))))
'accounts admin authtoken contenttypes sessions token (the rest are apps in my project)'

Ran

python manage.py makemigrations --replace-all --name replace_old_migration (everything from previous script)

All at once:

python \
   manage.py \
   makemigrations \
   --replace-all \
   --name replace_old_migrations \
   $(python manage.py shell -c "from django.apps import apps; print(' '.join(map(str, sorted({model._meta.app_label for model
in apps.get_models()}))))")

Error

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django_replace_migrations/management/commands/makemigrations.py", line 154, in handle
    loader.project_state(),
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 324, in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/graph.py", line 315, in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)
KeyError: ('contenttypes', '0001_initial')

The previous script is only for convenience. I manually wrote out the makemigrations line with all the app listed and I still got the same error. I removed each app that the key error was complaining about until I was down to like three apps before it happily ran.

Other strategies attempted

Even if I add if model._meta.app_label not in {'admin', 'auth', 'authtoken', 'contenttypes', 'sessions', 'token'} filter (apps which are not mine) I still get a KeyError, just with a different app.

Many of my apps depend on other apps, and many of those apps depend on each other.
Do I need to separate squashing into separate groups/batches? How would I tell which apps go in which group/batch?

Versions

Python: 3.6.8
Django: 2.2.1
django-replace-migrations: 0.0.2

Looks like this app is not working . i am seeing same issue

flixx commented

Hello @CelestialGuru @pupattan

Thanks for trying out my package! Indeed so far it was only tested with only one (big) codebase.

So the way it worked for us is that only those apps are listed which we control ourselves and not django core apps or other dependencies. In particular those apps must not be in the list: accounts admin authtoken contenttypes sessions token.
(Because you can not replace migrations of those. Not managed by you)

Crafting the app-list manually is a good way to go.

Can you confirm that the command also fails with only your apps?
Would you see the same error then or a different?

Best,
Felix

Same issue :(