diff --git a/InvenTree/part/migrations/0112_auto_20230525_1606.py b/InvenTree/part/migrations/0112_auto_20230525_1606.py index 612761a358..ff7d296336 100644 --- a/InvenTree/part/migrations/0112_auto_20230525_1606.py +++ b/InvenTree/part/migrations/0112_auto_20230525_1606.py @@ -1,8 +1,40 @@ # Generated by Django 3.2.19 on 2023-05-31 12:05 +from django.core.exceptions import FieldDoesNotExist from django.db import migrations, models +def delete_columns(apps, schema_editor): + """Hack method to delete columns (if they already exist). + + Due to an improper set of migrations merges, + there may exist a situation where the columns (defined in the migrations below) already exist. + + In this case, we want to delete the columns, and then re-add them. + + Original error: https://github.com/inventree/InvenTree/pull/4898 + Attempted fix: https://github.com/inventree/InvenTree/pull/4961 + This fix: https://github.com/inventree/InvenTree/pull/4977 + """ + + PartParameterTemplate = apps.get_model('part', 'PartParameterTemplate') + + # Check if the 'checkbox' column exists + try: + print("Checking for column 'checkbox' in table 'part_partparametertemplate'") + PartParameterTemplate._meta.get_field('checkbox') + schema_editor.execute("ALTER TABLE part_partparametertemplate DROP COLUMN checkbox;") + except (AttributeError, FieldDoesNotExist): + print("Column 'checkbox' does not exist (skipping)") + + try: + print("Checking for column 'choices' in table 'part_partparametertemplate'") + PartParameterTemplate._meta.get_field('choices') + schema_editor.execute("ALTER TABLE part_partparametertemplate DROP COLUMN choices;") + except (AttributeError, FieldDoesNotExist): + print("Column 'choices' does not exist (skipping)") + + class Migration(migrations.Migration): dependencies = [ @@ -10,6 +42,9 @@ class Migration(migrations.Migration): ] operations = [ + migrations.RunPython( + delete_columns, reverse_code=migrations.RunPython.noop + ), migrations.AddField( model_name='partparametertemplate', name='checkbox',