3YOURMIND / django-replace-migrations

Apache License 2.0
12 stars 7 forks source link

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

Open CelestialGuru opened 3 years ago

CelestialGuru commented 3 years ago

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

pupattan commented 3 years ago

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

flixx commented 3 years ago

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

narrowfail commented 3 years ago

Same issue :(

christianbundy commented 4 months ago

Confirming the same issue on Django 5:

$  ./manage.py makemigrations --replace-all --name replace my_custom_app
Traceback (most recent call last):
  File "/home/docker/code/./manage.py", line 28, in <module>
    execute_from_command_line(sys.argv)
  File "/.venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/.venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.12/site-packages/django_replace_migrations/management/commands/makemigrations.py", line 154, in handle
    loader.project_state(),
    ^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.12/site-packages/django/db/migrations/loader.py", line 361, in project_state
    return self.graph.make_state(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/.venv/lib/python3.12/site-packages/django/db/migrations/graph.py", line 329, in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)
                    ~~~~~~~~~~^^^^^^
KeyError: ('my_custom_app', '0001_squash')