Opened 2 years ago

Last modified 2 years ago

#33902 closed Bug

UniqueConstraint: 'F' object has no attribute 'flatten' — at Version 1

Reported by: Adam Zahradník Owned by: nobody
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.

Change History (1)

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

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top