diff --git a/specifyweb/specify/migrations/0007_tectonic_ranks.py b/specifyweb/specify/migrations/0007_tectonic_ranks.py index c861cb5f6b2..73c93fc98e5 100644 --- a/specifyweb/specify/migrations/0007_tectonic_ranks.py +++ b/specifyweb/specify/migrations/0007_tectonic_ranks.py @@ -55,12 +55,21 @@ def create_default_tectonic_ranks(apps): def revert_default_tectonic_ranks(apps, schema_editor): TectonicUnit = apps.get_model('specify', 'TectonicUnit') + TectonicUnitTreeDefItem = apps.get_model('specify', 'TectonicUnitTreeDefItem') TectonicTreeDef = apps.get_model('specify', 'TectonicUnitTreeDef') Discipline = apps.get_model('specify', 'Discipline') for discipline in Discipline.objects.all(): - tectonic_tree_def = TectonicTreeDef.objects.filter(name="Tectonic Unit", discipline=discipline) - if tectonic_tree_def: + tectonic_tree_defs = TectonicTreeDef.objects.filter(name="Tectonic Unit", discipline=discipline) + + for tectonic_tree_def in tectonic_tree_defs: + tectonic_unit_tree_def_items = TectonicUnitTreeDefItem.objects.filter(treedef=tectonic_tree_def).order_by('-id') + + for item in tectonic_unit_tree_def_items: + TectonicUnit.objects.filter(definitionitem=item).delete() + + item.delete() + tectonic_tree_def.delete() def create_root_tectonic_node(apps): @@ -93,6 +102,7 @@ def create_root_tectonic_node(apps): def revert_create_root_tectonic_node(apps, schema_editor): TectonicUnit = apps.get_model('specify', 'TectonicUnit') + TectonicUnitTreeDefItem = apps.get_model('specify', 'TectonicUnitTreeDefItem') TectonicTreeDef = apps.get_model('specify', 'TectonicUnitTreeDef') Discipline = apps.get_model('specify', 'Discipline') @@ -100,6 +110,7 @@ def revert_create_root_tectonic_node(apps, schema_editor): tectonic_tree_def = TectonicTreeDef.objects.filter(name="Tectonic Unit", discipline=discipline).first() if tectonic_tree_def: + TectonicUnitTreeDefItem.objects.filter(treedef=tectonic_tree_def).delete() TectonicUnit.objects.filter( name="Root" ).delete() @@ -116,7 +127,7 @@ def consolidated_python_django_migration_operations(apps, schema_editor): def revert_cosolidated_python_django_migration_operations(apps, schema_editor): revert_default_tectonic_ranks(apps, schema_editor) - revert_create_root_tectonic_node(apps) + revert_create_root_tectonic_node(apps, schema_editor) operations = [ migrations.RunPython(consolidated_python_django_migration_operations, revert_cosolidated_python_django_migration_operations, atomic=True),