From 8d9f2dce8cc4a150ffc614d0ace47c886ca43e06 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 20 Jul 2026 04:47:00 +0000 Subject: [PATCH] Apply fix for migration testing --- src/backend/InvenTree/generic/states/fields.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/backend/InvenTree/generic/states/fields.py b/src/backend/InvenTree/generic/states/fields.py index 0ca677b4ab..fc337bda58 100644 --- a/src/backend/InvenTree/generic/states/fields.py +++ b/src/backend/InvenTree/generic/states/fields.py @@ -5,6 +5,7 @@ from typing import Any, Optional from django.core.exceptions import ObjectDoesNotExist from django.db import models +from django.db.migrations.state import StateApps from django.utils.encoding import force_str from django.utils.translation import gettext_lazy as _ @@ -117,11 +118,22 @@ class InvenTreeCustomStatusModelField(models.PositiveIntegerField): return name, path, args, kwargs def contribute_to_class(self, cls, name): - """Add the _custom_key field to the model.""" + """Add the _custom_key field to the model. + + Historical model states (rendered by the migration framework) already + declare the '{name}_custom_key' field explicitly, as it was captured as + an AddField operation when the migration was generated. Auto-adding it + here too would create a duplicate field on those historical models, so + skip it for anything other than the 'live' application registry. + """ cls._meta.supports_custom_status = True - if not hasattr(self, '_custom_key_field') and not hasattr( - cls, f'{name}_custom_key' + is_historical = isinstance(cls._meta.apps, StateApps) + + if ( + not is_historical + and not hasattr(self, '_custom_key_field') + and not hasattr(cls, f'{name}_custom_key') ): self.add_field(cls, name)