Add nulls_distinct support to UniqueTogetherValidator#9866
Open
mag123c wants to merge 5 commits intoencode:mainfrom
Open
Add nulls_distinct support to UniqueTogetherValidator#9866mag123c wants to merge 5 commits intoencode:mainfrom
mag123c wants to merge 5 commits intoencode:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds support for Django 5.0's nulls_distinct parameter to the UniqueTogetherValidator class in Django REST Framework. When nulls_distinct=False is set on a UniqueConstraint, the validator now properly treats NULL values as equal for uniqueness checks, preventing integrity errors on databases like Oracle where NULLs can violate unique constraints.
Changes:
- Modified
get_unique_together_constraints()to extract and yield thenulls_distinctattribute from constraints - Updated
UniqueTogetherValidatorto accept and handle thenulls_distinctparameter in validation logic - Added comprehensive tests for the new functionality with Django 5.0+ version guards
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| rest_framework/serializers.py | Updated get_unique_together_constraints() to yield nulls_distinct from constraints and modified call sites to unpack the additional tuple element |
| rest_framework/validators.py | Added nulls_distinct parameter to UniqueTogetherValidator, updated validation logic to skip NULL checks when nulls_distinct=False, and modified __repr__() and __eq__() methods |
| tests/test_validators.py | Added test model with nulls_distinct=False constraint and comprehensive test cases for create operations and validator equality |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
auvipy
requested changes
Mar 3, 2026
Collaborator
auvipy
left a comment
There was a problem hiding this comment.
please cross check the suggestion and see if they are right to go with
Author
|
@auvipy Reviewed all 5 Copilot suggestions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8409
This PR adds support for Django 5.0's
nulls_distinctoption inUniqueTogetherValidator.Problem
When
nulls_distinct=Falseis set on aUniqueConstraint, DRF's validator still skips validation for NULL values, causing integrity errors on databases like Oracle where NULLs can violate unique constraints.Solution
nulls_distinctfromUniqueConstraintinget_unique_together_constraints()UniqueTogetherValidator.__init__nulls_distinct=False, validate NULL values as potential duplicatesChanges
rest_framework/serializers.py: Updatedget_unique_together_constraints()to yieldnulls_distinctrest_framework/validators.py: Addednulls_distinctparameter toUniqueTogetherValidatortests/test_validators.py: Added tests fornulls_distinct=Falsebehavior (Django 5.0+)Backward Compatibility
nulls_distinct=None(default): Existing behavior preserved (skip NULL validation)getattr()fallback, no breaking changes