Home > front end >  Django multitenant migration raises KeyError: "prune"
Django multitenant migration raises KeyError: "prune"

Time:06-10

So I've been working in a project for a while, and haven't really changed the models at all, and therefore haven't done any migrations. Now I needed to add two new fields and delete another one, which should be normally fine. I'm using django-tenants, so my command to run the migrations is ./manage.py migrate_schemas. Now, whenever I run that, I get the error KeyError: "prune" (the full traceback is below) in what seems to be internal code of Django.

Now, afterwards I tried reverting my changes, so no new migration and running the comnad again, and got the same error. I also thought that maybe the database had gotten "dirty" at some point, so I tried migrating a clean database from scratch, with the same result. The migrations don't even get to start.

Has anyone ever encountered something similar?

The full traceback (I have simplified the paths)

[standard:public] === Starting migration
Traceback (most recent call last):
  File ":directory/./manage.py", line 22, in <module>
    main()
  File ":directory/./manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "$venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "$/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "$venv/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "$venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "$venv/lib/python3.9/site-packages/django_tenants/management/commands/migrate_schemas.py", line 52, in handle
    executor.run_migrations(tenants=[self.PUBLIC_SCHEMA_NAME])
  File "$venv/lib/python3.9/site-packages/django_tenants/migration_executors/standard.py", line 11, in run_migrations
    run_migrations(self.args, self.options, self.codename, self.PUBLIC_SCHEMA_NAME)
  File "$venv/lib/python3.9/site-packages/django_tenants/migration_executors/base.py", line 53, in run_migrations
    MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)
  File "$venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "$venv/lib/python3.9/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "$venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 188, in handle
    if options["prune"]:
KeyError: 'prune'

CodePudding user response:

It seems like version incompatibility.

prune option added to Django couple months ago (Jan 22, 2022)

If you want to use newer Django you have to patch django-tenants manually and add --prune argument

def add_arguments(self, parser):
    parser.add_argument(
        '--prune', action='store_true', dest='prune',
        help='Delete nonexistent migrations from the django_migrations table.',
    )

PS I couldn't find issue related to prune, so you may create new one ;)

  • Related