Fix UniqueConstraint validation with conditional fields (#9707) - #10021
Fix UniqueConstraint validation with conditional fields (#9707)#10021majidkhazaei wants to merge 10 commits into
Conversation
…ogether validator
- 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>
There was a problem hiding this comment.
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 byUniqueConstraint.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 |
…eferenced_base_fields
|
I find the whole conditional uniqueness stuff confusing and don't have the conceptual clarity in my head atm to give this a solid review (I'm sorry). |
|
Hi @browniebroke, just a quick follow-up on this PR. The checks have been green for a while, and the branch is up to date. Whenever you have a chance, I’d really appreciate a review. If there’s anything I can adjust or clarify, please let me know. Thanks! |
| yield ( | ||
| constraint.fields, | ||
| model._default_manager, | ||
| condition_fields, | ||
| constraint.condition, |
| yield ( | ||
| constraint.fields, | ||
| model._default_manager, | ||
| condition_fields, |
There was a problem hiding this comment.
@majidkhazaei please crosscheck this and other open suggestions
There was a problem hiding this comment.
Thanks @auvipy!
Yes, I'm currently reviewing all the suggestions from Copilot. I'll go through them one by one and apply the necessary fixes (especially the ones regarding empty constraint.fields and condition field changes).
I'll push the updates later today and let you know once they're ready for another look.
Appreciate your patience and guidance!
ec2a480 to
1be7064
Compare
…t for custom messages, recheck condition field changes - Add check for empty constraint.fields in get_unique_together_constraints - Preserve original constraint object for custom violation messages/codes - Extend UniqueTogetherValidator to recheck when condition fields change - Add test for condition field change triggering revalidation All 68 tests passing.
1be7064 to
51b7897
Compare
|
Hi @auvipy and @browniebroke, All three Copilot issues are now fixed:
A new test was added for the condition field scenario. All 68 tests are passing. PR is ready for final review and merge. Thanks! 🙏 |
|
tried to fix a newly emerged merge conflict |
This is a rebased and conflict-resolved version of PR #9744.
Changes:
get_referenced_base_fields_from_qhelper torest_framework/compat.pytests/test_validators.pyare passingResolves #9707
Supersedes #9744 (with resolved conflicts)
When using Django's
UniqueConstraintwith conditions that reference other fields,DRF now correctly applies
UniqueTogetherValidatorinstead ofUniqueValidator.