django-sharding
django-sharding copied to clipboard
Router fails on migrations which contain deleted models
Hy. In one of migrations I have model, that has been deleted in later migrations and doesn't exist for now. When I do migrate, I get this error:
File "/home/sergei/venv36/lib/python3.6/site-packages/django/apps/config.py", line 163, in get_model
return self.models[model_name.lower()]
KeyError: 'clickhousecollapsemodelupdate'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/sergei/.pycharm_helpers/pycharm/django_manage.py", line 43, in <module>
run_module(manage_file, None, '__main__', True)
File "/usr/lib/python3.6/runpy.py", line 205, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/usr/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/vagrant/cqapi/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/sergei/venv36/lib/python3.6/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/sergei/venv36/lib/python3.6/site-packages/django_sharding_library/management/commands/migrate.py", line 21, in handle
super(Command, self).handle(*args, **options)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/home/sergei/venv36/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 95, in database_forwards
if self.allow_migrate_model(schema_editor.connection.alias, model):
File "/home/sergei/venv36/lib/python3.6/site-packages/django/db/migrations/operations/base.py", line 115, in allow_migrate_model
return router.allow_migrate_model(connection_alias, model)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/db/utils.py", line 311, in allow_migrate_model
model=model,
File "/home/sergei/venv36/lib/python3.6/site-packages/django/db/utils.py", line 300, in allow_migrate
allow = method(db, app_label, **hints)
File "/home/sergei/venv36/lib/python3.6/site-packages/django_sharding_library/router.py", line 157, in allow_migrate
raise exception
File "/home/sergei/venv36/lib/python3.6/site-packages/django_sharding_library/router.py", line 152, in allow_migrate
model = app.get_model(model_name)
File "/home/sergei/venv36/lib/python3.6/site-packages/django/apps/config.py", line 166, in get_model
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'utils' doesn't have a 'ClickHouseCollapseModelUpdate' model.
This I have a patch for, I just wanted to iterate in it before documenting it as I only discovered it not that long ago. It's similar to the issue where you use a third party package on a model and removing the package would make import errors happen on migrations.
https://github.com/JBKahn/django-sharding/pull/67 That's the temporary fix.
@JBKahn i think that's a common issue with a lot of custom/off script migrations, it seems like something they may want to try to fix at the django/migration level as I often do this myself.
The real issue is how do you know if you should consider something missing as "ok" or not. Maybe I can try looking up the Django migration history but then I don't know which database to look at. Very much a pain.