diff --git a/importer/migrations/0005_fixup_dynamic_shema_table_names.py b/importer/migrations/0005_fixup_dynamic_shema_table_names.py index e216c8ae..300eb547 100644 --- a/importer/migrations/0005_fixup_dynamic_shema_table_names.py +++ b/importer/migrations/0005_fixup_dynamic_shema_table_names.py @@ -1,26 +1,23 @@ from django.db import migrations import logging from django.db import ProgrammingError +from django.db import connections logger = logging.getLogger(__name__) def fixup_table_name(apps, schema_editor): try: - schema = apps.get_model("dynamic_models", "ModelSchema") - for val in schema.objects.all(): - if val.name != val.db_table_name: - val.db_table_name = val.name - val.save() - except ProgrammingError as e: """ - The dynamic model should exists to apply the above migration. + The dynamic model should exists to apply the migration. In case it does not exists we can skip it """ - if 'relation "dynamic_models_modelschema" does not exist' in e.args[0]: - logging.debug("Dynamic model does not exists yet, skipping") - return - raise e + if 'dynamic_models_modelschema' in schema_editor.connection.introspection.table_names(): + schema = apps.get_model("dynamic_models", "ModelSchema") + for val in schema.objects.all(): + if val.name != val.db_table_name: + val.db_table_name = val.name + val.save() except Exception as e: raise e