Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.",
),
),
]
9 changes: 7 additions & 2 deletions arches/extensions/controlled_lists/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _

Expand Down Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions releases/8.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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

Expand Down
76 changes: 75 additions & 1 deletion tests/extensions/controlled_lists/test_models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down
Loading