Skip to content

Fix UniqueConstraint validation with conditional fields (#9707) - #10021

Open
majidkhazaei wants to merge 6 commits into
encode:mainfrom
majidkhazaei:pr-9744
Open

Fix UniqueConstraint validation with conditional fields (#9707)#10021
majidkhazaei wants to merge 6 commits into
encode:mainfrom
majidkhazaei:pr-9744

Conversation

@majidkhazaei

Copy link
Copy Markdown

This is a rebased and conflict-resolved version of PR #9744.

Changes:

  • Added get_referenced_base_fields_from_q helper to rest_framework/compat.py
  • Fixed test indentation issues (moved test methods inside the correct class)
  • All 67 tests in tests/test_validators.py are passing

Resolves #9707
Supersedes #9744 (with resolved conflicts)

When using Django's UniqueConstraint with conditions that reference other fields,
DRF now correctly applies UniqueTogetherValidator instead of UniqueValidator.

nefrob and others added 4 commits August 13, 2026 16:08
- Add helper function for extracting fields from Q objects
- Move test methods inside TestUniquenessTogetherValidation class
- All 67 tests passing

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@auvipy
auvipy requested review from auvipy and a lite review from Copilot August 13, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes DRF’s ModelSerializer uniqueness validation for Django UniqueConstraint objects whose condition references additional model fields, ensuring DRF uses serializer-level UniqueTogetherValidator (with condition-awareness) instead of an incorrect field-level UniqueValidator.

Changes:

  • Add get_referenced_base_fields_from_q() compatibility helper and use it to detect fields referenced by UniqueConstraint.condition.
  • Update uniqueness validator selection so single-field constraints with distinct condition fields are validated via UniqueTogetherValidator.
  • Extend validator test coverage and document the UniqueConstraint-with-conditions behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
rest_framework/compat.py Adds helper to extract referenced base fields from Q conditions.
rest_framework/utils/field_mapping.py Skips field-level UniqueValidator when condition references additional fields.
rest_framework/serializers.py Treats certain single-field conditional UniqueConstraints as “unique-together” for serializer-level validation.
tests/test_validators.py Adds/adjusts tests for conditional-field uniqueness behavior and expected validator placement.
docs/api-guide/validators.md Documents how DRF handles UniqueConstraint conditions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

]


## Updating nested serializers
Comment thread rest_framework/compat.py Outdated
@auvipy auvipy added this to the 3.18 milestone Aug 13, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Serializer UniqueConstraint validation fails incorrectly on create with conditional fields

4 participants