Skip to content

Add nulls_distinct support to UniqueTogetherValidator#9866

Open
mag123c wants to merge 5 commits intoencode:mainfrom
mag123c:fix/nulls-distinct-validator-8409
Open

Add nulls_distinct support to UniqueTogetherValidator#9866
mag123c wants to merge 5 commits intoencode:mainfrom
mag123c:fix/nulls-distinct-validator-8409

Conversation

@mag123c
Copy link

@mag123c mag123c commented Jan 12, 2026

Fixes #8409

This PR adds support for Django 5.0's nulls_distinct option in UniqueTogetherValidator.

Problem

When nulls_distinct=False is set on a UniqueConstraint, DRF's validator still skips validation for NULL values, causing integrity errors on databases like Oracle where NULLs can violate unique constraints.

Solution

  • Extract nulls_distinct from UniqueConstraint in get_unique_together_constraints()
  • Pass it to UniqueTogetherValidator.__init__
  • When nulls_distinct=False, validate NULL values as potential duplicates

Changes

  • rest_framework/serializers.py: Updated get_unique_together_constraints() to yield nulls_distinct
  • rest_framework/validators.py: Added nulls_distinct parameter to UniqueTogetherValidator
  • tests/test_validators.py: Added tests for nulls_distinct=False behavior (Django 5.0+)

Backward Compatibility

  • nulls_distinct=None (default): Existing behavior preserved (skip NULL validation)
  • Django < 5.0: Uses getattr() fallback, no breaking changes

Copy link
Contributor

Copilot AI left a comment

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 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 the nulls_distinct attribute from constraints
  • Updated UniqueTogetherValidator to accept and handle the nulls_distinct parameter 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.

Copy link
Collaborator

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

please cross check the suggestion and see if they are right to go with

@mag123c
Copy link
Author

mag123c commented Mar 6, 2026

@auvipy Reviewed all 5 Copilot suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UniqueTogetherValidator doesn't enforce uniqueness of NULL values on databases that DO enforce them

3 participants