#33902: UniqueConstraint: 'F' object has no attribute 'flatten'
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  gardenerik                         |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33902>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070182752dd7e0-9fcbf438-4d1e-473f-89bd-3c280466f406-000000%40eu-central-1.amazonses.com.

Reply via email to