Skip to content

Serializer UniqueConstraint validation fails incorrectly on create with conditional fields #9707

Closed

Description

@nefrob

Issue:

Adding the following test to TestUniqueConstraintValidation demonstrates an issue in the serializer’s unique constraint validation logic:

def test_unique_constraint_create(self):
    class SourceUniqueConstraintSerializer(serializers.ModelSerializer):
        raceName = serializers.CharField(source="race_name")

        class Meta:
            model = UniqueConstraintModel
            fields = ("raceName", "position", "global_id", "fancy_conditions")

    UniqueConstraintModel.objects.exclude(pk=self.instance.pk).delete()
    data = {
        "race_name": "other",
        "position": 1,
        "global_id": 3,
        "fancy_conditions": 1,
    }

    obj = UniqueConstraintModel.objects.create(**data)
    obj.delete()

    serializer = SourceUniqueConstraintSerializer(data=data)
    assert serializer.is_valid() # This unexpectedly fails
  • Creating a UniqueConstraintModel instance directly with UniqueConstraintModel.objects.create() succeeds, indicating no unique constraint violation occurs at the database level.
  • However, validating the same data through the ModelSerializer fails.

Root cause:
The unique validator used by the serializer does not account for the global_id field during validation of fancy_conditions, even though global_id is part of the conditional logic in the unique constraint.

As a result, the validation incorrectly identifies a conflict based only on fancy_conditions, without considering that the global_id in the input data should exempt it from the constraint.

Expected behavior:

Serializer validation should succeed in this case, consistent with the model-level behavior.

Activity

  1. changed the title [-]Serializer `UniqueConstraint` validation fails on create with conditional fields[/-] [+]Serializer `UniqueConstraint` validation fails incorrectly on create with conditional fields[/+] on May 23, 2025
  2. auvipy commented on Jun 2, 2025

    @auvipy
    Collaborator

    can you check this PR please #9712 ?

  3. nefrob commented on Jun 6, 2025

    @nefrob
    ContributorAuthor

    @auvipy this unfortunately doesn't resolve the issue. I think the failure happens in the unique validator call since does not consider the fields included in constraint conditions.

    https://github.com/kalekseev/django-rest-framework/blob/f30c0e2eedda410a7e6a0d1b351377a9084361b4/rest_framework/validators.py#L66

  4. auvipy commented on Jun 11, 2025

    @auvipy
    Collaborator

    Do you have the capacity to fix this?

  5. nefrob commented on Jun 11, 2025

    @nefrob
    ContributorAuthor

    @auvipy I might have some time in the next few weeks to take a look.

  6. nefrob commented on Jul 20, 2025

    @nefrob
    ContributorAuthor

    @auvipy tentative fix now up here: #9744.

  7. Adiorz commented on Oct 16, 2025

    @Adiorz

    @nefrob @auvipy Do you intend to continue working on this?

  8. nefrob commented on Oct 16, 2025

    @nefrob
    ContributorAuthor

    @Adiorz just looking for desired direction. If we want something more comprehensive / specific than the above PR, I probably won't have time and would defer to someone else. I can make a few small adjustments on the existing approach if needed however.

  9. ElavarasiArumugam commented on Jan 27, 2026

    @ElavarasiArumugam

    Hi! I'm a student preparing for open source contributions and I'd love to work on this issue. Could I take this? Thanks!

  10. added a commit that references this issue on Apr 6, 2026
    6459187
  11. added this to the 3.18 milestone on Sep 6, 2026
  12. added theissue type on Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions