Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33902 closed Bug (fixed)

UniqueConstraint's validation crashes with string expressions.

Reported by: Adam Zahradník Owned by: Mariusz Felisiak
Component: Database layer (models, ORM) Version: 4.1
Severity: Release blocker Keywords:
Cc: Gagaro Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Adam Zahradník)

When using UniqueConstraint with field names in expressions in Django 4.1, an error is raised when the model is saved:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 686, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 1890, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "/usr/local/lib/python3.10/site-packages/django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 1750, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/admin/options.py", line 1796, in _changeform_view
    form_validated = form.is_valid()
  File "/usr/local/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid
    return self.is_bound and not self.errors
  File "/usr/local/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors
    self.full_clean()
  File "/usr/local/lib/python3.10/site-packages/django/forms/forms.py", line 439, in full_clean
    self._post_clean()
  File "/usr/local/lib/python3.10/site-packages/django/forms/models.py", line 492, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 1491, in full_clean
    self.validate_constraints(exclude=exclude)
  File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 1442, in validate_constraints
    constraint.validate(model_class, self, exclude=exclude, using=using)
  File "/usr/local/lib/python3.10/site-packages/django/db/models/constraints.py", line 329, in validate
    for expr in expression.flatten():
AttributeError: 'F' object has no attribute 'flatten'

The culprit is most probably the new constraint validation (call to self.validate_constraints in the traceback).

This is excerpt from the model in question:

class Model:
    class Meta:
        constraints = (
            UniqueConstraint(
                "field1",
                "field2",
                name="some_constraint_name",
            ),
        )

Please note, that I am not using the fields keyword argument, but the positional expressions.

I can confirm that the same model works in Django 4.0.7.

Attachments (1)

ticket-33902-regression.diff (981 bytes ) - added by Mariusz Felisiak 2 years ago.
Regression test.

Download all attachments as: .zip

Change History (6)

comment:1 by Adam Zahradník, 2 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 2 years ago

Cc: Gagaro added
Severity: NormalRelease blocker
Summary: UniqueConstraint: 'F' object has no attribute 'flatten'UniqueConstraint's validation crashes with string expressions.
Triage Stage: UnreviewedAccepted

Thanks for the report! Bug in 667105877e6723c6985399803a364848891513cc.

by Mariusz Felisiak, 2 years ago

Regression test.

comment:3 by Mariusz Felisiak, 2 years ago

Has patch: set
Owner: changed from nobody to Mariusz Felisiak
Status: newassigned

comment:4 by GitHub <noreply@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 6388482:

Fixed #33902 -- Fixed Meta.constraints validation crash with F() expressions.

Thanks Adam Zahradník for the report.

Bug in 667105877e6723c6985399803a364848891513cc.

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

In 6b019314:

[4.1.x] Fixed #33902 -- Fixed Meta.constraints validation crash with F() expressions.

Thanks Adam Zahradník for the report.

Bug in 667105877e6723c6985399803a364848891513cc.
Backport of 63884829acd207404f2a5c3cc1d6b4cd0a822b70 from main

Note: See TracTickets for help on using tickets.
Back to Top