#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 )
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)
Change History (6)
comment:1 by , 2 years ago
Description: | modified (diff) |
---|
comment:2 by , 2 years ago
Cc: | added |
---|---|
Severity: | Normal → Release blocker |
Summary: | UniqueConstraint: 'F' object has no attribute 'flatten' → UniqueConstraint's validation crashes with string expressions. |
Triage Stage: | Unreviewed → Accepted |
Note:
See TracTickets
for help on using tickets.
Thanks for the report! Bug in 667105877e6723c6985399803a364848891513cc.