You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Serializer UniqueConstraint validation fails incorrectly on create with conditional fields #9707
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.
Reacted by Sergei Aleshin, Luca Da Rin Fioretto, Adiorz, Shad and Maxximus
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
@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.
@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.
Activity
[-]Serializer `UniqueConstraint` validation fails on create with conditional fields[/-][+]Serializer `UniqueConstraint` validation fails incorrectly on create with conditional fields[/+]auvipy commentedon Jun 2, 2025
can you check this PR please #9712 ?
nefrob commentedon Jun 6, 2025
@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
auvipy commentedon Jun 11, 2025
Do you have the capacity to fix this?
nefrob commentedon Jun 11, 2025
@auvipy I might have some time in the next few weeks to take a look.
nefrob commentedon Jul 20, 2025
@auvipy tentative fix now up here: #9744.
Adiorz commentedon Oct 16, 2025
@nefrob @auvipy Do you intend to continue working on this?
nefrob commentedon Oct 16, 2025
@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.
ElavarasiArumugam commentedon Jan 27, 2026
Hi! I'm a student preparing for open source contributions and I'd love to work on this issue. Could I take this? Thanks!
Fix encode#9707: UniqueConstraint validation on create with condition…
Fix UniqueConstraint validation with conditional fields (#9707) (#10021)
Fix UniqueConstraint validation with conditional fields (encode#9707) (…