From 07f060545b5f8b3567599104fece5a43ed593a13 Mon Sep 17 00:00:00 2001 From: Alexei Peters Date: Thu, 29 Oct 2026 16:39:47 -0700 Subject: [PATCH 1/3] use md5 hash for controlled list item value, re #13086 --- ...13_hash_listitemvalue_unique_constraint.py | 30 ++++++++ arches/extensions/controlled_lists/models.py | 9 ++- .../controlled_lists/test_models.py | 76 ++++++++++++++++++- 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 arches/extensions/controlled_lists/migrations/0013_hash_listitemvalue_unique_constraint.py diff --git a/arches/extensions/controlled_lists/migrations/0013_hash_listitemvalue_unique_constraint.py b/arches/extensions/controlled_lists/migrations/0013_hash_listitemvalue_unique_constraint.py new file mode 100644 index 0000000000..b496c04027 --- /dev/null +++ b/arches/extensions/controlled_lists/migrations/0013_hash_listitemvalue_unique_constraint.py @@ -0,0 +1,30 @@ +# Generated by Django 6.1 on 2026-10-29 18:28 + +import django.db.models.functions.text +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("arches_controlled_lists", "0012_fix_whitespace_v2"), + ("models", "13002_tile_and_resource_row_metadata"), + ] + + operations = [ + migrations.RemoveConstraint( + model_name="listitemvalue", + name="unique_item_value_valuetype_language", + ), + migrations.AddConstraint( + model_name="listitemvalue", + constraint=models.UniqueConstraint( + models.F("list_item"), + django.db.models.functions.text.MD5("value"), + models.F("valuetype"), + models.F("language"), + name="unique_item_value_valuetype_language", + violation_error_message="The same item value cannot be stored twice in the same language.", + ), + ), + ] diff --git a/arches/extensions/controlled_lists/models.py b/arches/extensions/controlled_lists/models.py index 12c873647d..02d6cf41ec 100644 --- a/arches/extensions/controlled_lists/models.py +++ b/arches/extensions/controlled_lists/models.py @@ -11,7 +11,8 @@ from django.core.exceptions import ValidationError from django.core.validators import MinValueValidator from django.db import models, transaction -from django.db.models import Deferrable, Q +from django.db.models import Deferrable, F, Q +from django.db.models.functions import MD5 from django.utils import translation from django.utils.translation import gettext_lazy as _ @@ -544,7 +545,11 @@ class ListItemValue(models.Model): class Meta: constraints = [ models.UniqueConstraint( - fields=["list_item", "value", "valuetype", "language"], + F("list_item"), + # Hash long notes to stay within PostgreSQL's B-tree entry limit. + MD5("value"), + F("valuetype"), + F("language"), name="unique_item_value_valuetype_language", violation_error_message=_( "The same item value cannot be stored twice in the same language." diff --git a/tests/extensions/controlled_lists/test_models.py b/tests/extensions/controlled_lists/test_models.py index c572b820e6..7d41aa12a2 100644 --- a/tests/extensions/controlled_lists/test_models.py +++ b/tests/extensions/controlled_lists/test_models.py @@ -1,9 +1,12 @@ +import hashlib import time from django.conf import settings +from django.core.exceptions import ValidationError +from django.db import IntegrityError, transaction from django.test import TestCase -from arches.app.models.models import DValueType +from arches.app.models.models import DValueType, Language from arches.app.search.elasticsearch_dsl_builder import Query from arches.app.search.search_engine_factory import SearchEngineInstance from arches.extensions.controlled_lists.models import List, ListItem, ListItemValue @@ -25,6 +28,77 @@ def test_uri_generation_guards_against_failure(self): self.assertIsNotNone(item.uri) +class ListItemValueTests(TestCase): + @classmethod + def setUpTestData(cls): + test_list = List(name="Test List") + List.objects.bulk_create([test_list]) + cls.items = [ + ListItem(list=test_list, sortorder=index, uri=f"http://example.com/{index}") + for index in range(2) + ] + ListItem.objects.bulk_create(cls.items) + for language in ("en", "de"): + Language.objects.get_or_create(code=language) + cls.value_fields = { + "list_item": cls.items[0], + "valuetype": DValueType.objects.get(valuetype="note"), + "language_id": "en", + } + # Varied text avoids compression hiding PostgreSQL's index-entry limit. + cls.long_value = "".join( + hashlib.sha256(str(index).encode()).hexdigest() for index in range(512) + ) + + def test_long_note_is_stored_without_truncation(self): + value = ListItemValue(**self.value_fields, value=self.long_value) + value.full_clean() + ListItemValue.objects.bulk_create([value]) + value.refresh_from_db() + self.assertEqual(value.value, self.long_value) + + def test_duplicate_long_note_is_rejected_by_database(self): + ListItemValue.objects.bulk_create( + [ListItemValue(**self.value_fields, value=self.long_value)] + ) + with self.assertRaises(IntegrityError), transaction.atomic(): + ListItemValue.objects.bulk_create( + [ListItemValue(**self.value_fields, value=self.long_value)] + ) + + def test_duplicate_long_note_is_rejected_by_validation(self): + ListItemValue.objects.bulk_create( + [ListItemValue(**self.value_fields, value=self.long_value)] + ) + duplicate = ListItemValue(**self.value_fields, value=self.long_value) + with self.assertRaisesMessage( + ValidationError, + "The same item value cannot be stored twice in the same language.", + ): + duplicate.full_clean() + + def test_different_values_items_types_and_languages_remain_allowed(self): + ListItemValue.objects.bulk_create( + [ListItemValue(**self.value_fields, value=self.long_value)] + ) + alternatives = [ + {"value": self.long_value + "different"}, + {"list_item": self.items[1]}, + {"valuetype": DValueType.objects.get(valuetype="scopeNote")}, + {"language_id": "de"}, + ] + for alternative in alternatives: + with self.subTest(alternative=tuple(alternative)): + fields = { + **self.value_fields, + "value": self.long_value, + **alternative, + } + value = ListItemValue(**fields) + value.full_clean() + ListItemValue.objects.bulk_create([value]) + + class ListItemGetChildUrisTests(TestCase): @classmethod def setUpTestData(cls): From b951539bc2805882ba0dce29ba5588ecc0c80ca2 Mon Sep 17 00:00:00 2001 From: Alexei Peters Date: Thu, 29 Oct 2026 16:46:07 -0700 Subject: [PATCH 2/3] add release notes entry, re #13086 --- releases/8.2.0.md | 1 + 1 file changed, 1 insertion(+) diff --git a/releases/8.2.0.md b/releases/8.2.0.md index 9291c5965b..3d8bf5883f 100644 --- a/releases/8.2.0.md +++ b/releases/8.2.0.md @@ -76,6 +76,7 @@ - Under the default-deny permission framework, a resource's principal user can see it through the Elasticsearch-backed instance filter (used by `/geojson` and vector tiles), matching search and the database fallback [#13012](https://github.com/archesproject/arches/pull/13012) - Fix `__arches_slugify` keeping hyphens, which produced node aliases that could not be used as attribute names (e.g. in arches-querysets `aliased_data`); relational views and their columns are now named by node alias [#12773](https://github.com/archesproject/arches/pull/12773) - Fix the maplibre-gl "Critical dependency: the request of a dependency is an expression" webpack warning; webpack had replaced maplibre's worker URL code with a lookup that throws when static files are served from another origin [#13038](https://github.com/archesproject/arches/issues/13038) +- Fix saving or importing long controlled-list values failing with PostgreSQL's B-tree index size limit; the uniqueness constraint now indexes an MD5 hash of the value while preserving the full text and duplicate protection [#13086](https://github.com/archesproject/arches/issues/13086) ### Deprecations From 0e9e069a4b4143498442d71fa61e0b1938dbfa3a Mon Sep 17 00:00:00 2001 From: Alexei Peters Date: Fri, 30 Oct 2026 09:48:56 -0700 Subject: [PATCH 3/3] Update releases/8.2.0.md Co-authored-by: Cyrus Hiatt --- releases/8.2.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releases/8.2.0.md b/releases/8.2.0.md index 3d8bf5883f..6c5b55d7d9 100644 --- a/releases/8.2.0.md +++ b/releases/8.2.0.md @@ -76,7 +76,7 @@ - Under the default-deny permission framework, a resource's principal user can see it through the Elasticsearch-backed instance filter (used by `/geojson` and vector tiles), matching search and the database fallback [#13012](https://github.com/archesproject/arches/pull/13012) - Fix `__arches_slugify` keeping hyphens, which produced node aliases that could not be used as attribute names (e.g. in arches-querysets `aliased_data`); relational views and their columns are now named by node alias [#12773](https://github.com/archesproject/arches/pull/12773) - Fix the maplibre-gl "Critical dependency: the request of a dependency is an expression" webpack warning; webpack had replaced maplibre's worker URL code with a lookup that throws when static files are served from another origin [#13038](https://github.com/archesproject/arches/issues/13038) -- Fix saving or importing long controlled-list values failing with PostgreSQL's B-tree index size limit; the uniqueness constraint now indexes an MD5 hash of the value while preserving the full text and duplicate protection [#13086](https://github.com/archesproject/arches/issues/13086) +- Fix saving long controlled-list values failing with PostgreSQL's B-tree index size limit. The uniqueness constraint now indexes an MD5 hashed value instead of the value itself while preserving the full text [#13086](https://github.com/archesproject/arches/issues/13086) ### Deprecations