Skip to content

Django 4.1 unique constraint failing #594

Open
@ash-plunkett-alliance

Description

@ash-plunkett-alliance

Recently we've upgraded our project to Django 4.1 (and Silk to 5.0.1 from 4.3.0), and a unique constraint on a model has begun to fail whilst Silk is enabled.

class MyModel(models.Model):
    name = models.CharField(max_length=255, blank=True)

    class Meta:
        constraints = [
            UniqueConstraint(
                fields=["name"],
                condition=~Q(name=""),
                name="unique_name_if_provided",
            ),
        ]

The error is being raised here: silk/sql.py:94

Full stack trace:

Traceback (most recent call last):
  File "/path/to/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/path/to/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/path/to/python3.10/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/path/to/python3.10/site-packages/django/contrib/auth/mixins.py", line 109, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/path/to/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
  File "...", line 640, in post
    return super().post(request, *args, **kwargs)
  File "...", line 609, in post
    if form.is_valid():
  File "/path/to/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid
    return self.is_bound and not self.errors
  File "/path/to/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors
    self.full_clean()
  File "/path/to/lib/python3.10/site-packages/django/forms/forms.py", line 439, in full_clean
    self._post_clean()
  File "/path/to/lib/python3.10/site-packages/django/forms/models.py", line 492, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "/path/to/python3.10/site-packages/django/db/models/base.py", line 1491, in full_clean
    self.validate_constraints(exclude=exclude)
  File "/path/to/python3.10/site-packages/django/db/models/base.py", line 1442, in validate_constraints
    constraint.validate(model_class, self, exclude=exclude, using=using)
  File "/path/to/python3.10/site-packages/django/db/models/constraints.py", line 358, in validate
    if (self.condition & Exists(queryset.filter(self.condition))).check(
  File "/path/to/python3.10/site-packages/django/db/models/query_utils.py", line 137, in check
    return compiler.execute_sql(SINGLE) is not None
  File "/path/to/python3.10/site-packages/silk/sql.py", line 94, in execute_sql
    if self.query.model.__module__ != 'silk.models':

Exception Type: AttributeError at ...
Exception Value: 'NoneType' object has no attribute '__module__'

And finally, the query attempted to be run:

SELECT 1 AS "_check" WHERE (NOT (provided_name = ) AND EXISTS(SELECT 1 AS "a" FROM "..." U0 WHERE (U0."name" = provided_name AND NOT (U0."name" = )) LIMIT 1))

Prior to the Django 4.1 upgrade, this constraint was working without a hitch with Silk running.

All other unique constraints within the system appear to still be functioning fine, including two other unique constraints on this same model, but this one with condition=~Q() is failing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions