-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Unique validation doesn't verify uniqueness between data items when many=True #6395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'd like to try solving this issue if it's considered valid, but I'm not sure what would be the best design choice to address it. The test I added is together with the |
My thoughts on this:
|
Evidently it is a valid issue yeah, tho it's more a case of "is there a simple enough pull request that would resolve this, or do we just accept it as a constraint and document it appropriately." I'm slightly surprised to not see this issue raised previously. There's a couple of similar cases that consider it from the POV of race requests with unique values. #3876, #5760 - Tho I think the answer to that is "use ATOMIC_REQUESTS = True" so that everything runs within a database transaction always.
Not sure, sounds fiddly either way around. The best way to make some progress towards this would probably be to consider how it'd be resolved with a plain old serializer class first, rather than with a modelserializer. If we start instead with this: class UniquenessModel(models.Model):
username = models.CharField(unique=True, max_length=100)
class UniquenessSerializer(serializers.Serializer):
username = models.CharField(max_length=100, validators=[
UniqueValidator(queryset=UniquenessModel.objects.all())
]) Then what's the best resolution for ensuring that your example case in #6396 would pass? |
On my real life case I just created a list serializer and added this logic to it's
|
The shortest solution I could think of for now was adding this valitator to the list serializer:
The error message formatting is probably wrong, but that'd be the general idea. To make this generic to all list serializers with child model serializers, the validator's |
@tomchristie this has not be risen earlier because it's a bit out of the scope of the default generic views which don't do batch operation on their resources. |
I'm having the same issue with the Serializer.is_valid method, any luck? |
|
thanks. based on this I think we can close the issue for now. please report back if anything break again |
what do you think about this PR https://github.com/encode/django-rest-framework/pull/6396/files ? should we add the test cases to see that it is actually working as per your report? |
Hi. I'm facing this issue as well. Since it's been first mentioned in 2019 and it seems that it was finally fixed about a month ago, will there be a release shipping this fix any time soon? |
Which version of django you are using . I tried with latest version last time |
Django 4.2 .8 It seems DRF has no newer version yet. |
Hi @auvipy @harshavardhanpillimitla , I don't think this issue should be closed as fixed (is it not planned instead?). The case #6395 (comment) still fails on master - |
Correct. It was closed as fixed, and then reverted. |
Checklist
master
branch of Django REST framework.Steps to reproduce
many=True
and adata
argument containing items with non unique values for the unique field.is_valid()
method - it will returnTrue
.save()
method - will raiseIntegrityError
Example
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.
The text was updated successfully, but these errors were encountered: