Skip to content

Unique validation doesn't verify uniqueness between data items when many=True #6395

Closed as not planned
@maribedran

Description

@maribedran

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

  • Create a model with an unique field
  • Create a serializer for the model
  • Instantiate the serializer with many=True and a data argument containing items with non unique values for the unique field
  • Call .is_valid() method - it will return True
  • Call .save() method - will raise IntegrityError

Example

class UniquenessModel(models.Model):
    username = models.CharField(unique=True, max_length=100)

class UniquenessSerializer(serializers.ModelSerializer):
    class Meta:
        model = UniquenessModel
        fields = '__all__'

data = [{'username': 'non-existing'}, {'username': 'non-existing'}]
serializer = UniquenessSerializer(data=data, many=True)
serializer.is_valid()   # True
serializer.save()  # django.db.utils.IntegrityError: UNIQUE constraint failed: tests_uniquenessmodel.username

Expected behavior

The serializer should validate the uniqueness between data items and the existing database entries as well as between themselves.

Actual behavior

The serializer only validates uniqueness between individual data items and database entries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions