diff --git a/arkindex/training/serializers.py b/arkindex/training/serializers.py index 33365ecfd81aa16bb3e02333a8bda96ae335dbdc..9686bbb12496048efddacb47275206d6e0826b39 100644 --- a/arkindex/training/serializers.py +++ b/arkindex/training/serializers.py @@ -791,6 +791,20 @@ class SelectionDatasetElementSerializer(serializers.Serializer): raise ValidationError(f"Dataset {set.dataset.id} is not part of corpus {corpus.name}.") if set.dataset.state == DatasetState.Complete: raise ValidationError(f"Dataset {set.dataset.id} is marked as completed.") + # Ensure adding elements to the dataset does not break uniqueness constraint + selection = self.context["request"].user.selected_elements.filter(corpus=corpus) + if set.dataset.unique_elements and ( + existing_set := ( + set.dataset.sets + .exclude(id=set.id) + .filter(set_elements__element_id__in=selection.values_list("id", flat=True)) + .values_list("name", flat=True) + .first() + ) + ): + raise ValidationError([ + f"The dataset prevent duplication and this element is already present in set {existing_set}." + ]) return set def create(self, validated_data):