Skip to content

Conversation

sshishov
Copy link
Contributor

@sshishov sshishov commented Aug 2, 2025

Description

This MR should fix the issue with blank=True field in UniqueTogether constraint. It should not become required all of a sudden.

Fixes #9750

Signed-off-by: Sergei Shishov <sshishov.sshishov@gmail.com>
@sshishov sshishov force-pushed the ss/fix/allow-blank-unique-constraint branch from 3bcd191 to 8de6a6c Compare August 2, 2025 12:56
@sshishov
Copy link
Contributor Author

sshishov commented Aug 2, 2025

I have added tests similar to the tests added in #9531

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.

did you check this pr #9725 ?

class Meta:
constraints = [
# Unique constraint on 2 blank fields
models.UniqueConstraint(name='unique_constraint', fields=('age', 'tag'), condition=~models.Q(models.Q(title='') & models.Q(tag='True')))
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we have a char length limit here?


def test_blank_uqnique_constraint_fields_are_not_required(self):
serializer = UniqueConstraintBlankSerializer(data={'age': 25})
self.assertTrue(serializer.is_valid(), serializer.errors)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we split it into multiple assertions?

ids_in_qs = {frozenset(v.queryset.values_list(flat=True)) for v in validators if hasattr(v, "queryset")}
assert ids_in_qs == {frozenset([1]), frozenset([3])}

def test_blank_uqnique_constraint_fields_are_not_required(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo here

@mahdighadiriii
Copy link

Hi @sshishov @auvipy @deepakangadi,
I'm excited to contribute to DRF as my first contribution! I noticed this PR (#9751) addresses a critical issue (#9750) with blank=True fields in UniqueConstraints, which aligns with my experience in Django/DRF serializers and API optimization.

I can help unblock this by:

  • Fixing the typo in test_blank_uqnique_constraint_fields_are_not_required (uqniqueunique).
  • Splitting assertions in the test for clarity, as suggested by @deepakangadi.
  • Adding char length limit (e.g., max_length=255) to the constraint.
  • Resolving conflicts by rebasing on main.
  • Checking for overlap with PR docs: unique constraints cause 'required=True' #9725, as mentioned by @auvipy.

If this sounds good, I can start working on these changes. Should I update this branch (if permitted) or create a new PR? Thanks for your guidance!

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.

3.15 is raising required error on model blank fields
4 participants