Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 4.1 unique constraint failing #594

Open
ash-plunkett-alliance opened this issue Aug 24, 2022 · 5 comments · Fixed by #618
Open

Django 4.1 unique constraint failing #594

ash-plunkett-alliance opened this issue Aug 24, 2022 · 5 comments · Fixed by #618
Labels

Comments

@ash-plunkett-alliance
Copy link

ash-plunkett-alliance commented Aug 24, 2022

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.

@Waszker
Copy link

Waszker commented Sep 5, 2022

I also experience the same issue - constraints are causing the exceptions. It looks like the query issued by them is not linked to any model.

@utapyngo
Copy link
Member

I see some commits here. When will a new version be released?

@albertyw
Copy link
Member

Pending #622

@utapyngo
Copy link
Member

#439 is older than 2 years. Do you really want to postpone a release because of it?

@albertyw
Copy link
Member

No, I don't. I think we should do a release asap.

@SebCorbin SebCorbin linked a pull request Nov 19, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants