question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SafeDeleteQuery AttributeError on _safedelete_visibility and _safedelete_visibility_field

See original GitHub issue

Hi, for certain queries I’m getting an AttributeError on the SafeDeleteQuery object. I’m running on the following dependencies:

Django==4.1
django-safedelete==1.3.0

The error I’m receiving has the following stack trace:

Internal Server Error: /company/
Traceback (most recent call last):
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1433, in build_filter
    join_info = self.setup_joins(
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1808, in setup_joins
    path, final_field, targets, rest = self.names_to_path(
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1733, in names_to_path
    raise MultiJoin(pos + 1, names_with_path)
django.db.models.sql.datastructures.MultiJoin: (1, [('billing_location_for_company', [PathInfo(from_opts=<Options for Location>, to_opts=<Options for Company>, target_fields=(<django.db.models.fields.BigAutoField: id>,), join_field=<ManyToOneRel: company.company>, m2m=True, direct=False, filtered_relation=None)])])

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "~/django-safedelete-bug/safedeletebug/company/views.py", line 6, in index
    locations = Location.objects.filter(
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1428, in exclude
    return self._filter_or_exclude(True, args, kwargs)
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1438, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1443, in _filter_or_exclude_inplace
    self._query.add_q(~Q(*args, **kwargs))
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1532, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1562, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1451, in build_filter
    return self.split_exclude(filter_expr, can_reuse, e.names_with_path)
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 2029, in split_exclude
    condition, needed_inner = self.build_filter(Exists(query))
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/expressions.py", line 1476, in __init__
    super().__init__(queryset, **kwargs)
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/django/db/models/expressions.py", line 1423, in __init__
    self.query = getattr(queryset, "query", queryset).clone()
  File "~/django-safedelete-bug/venv/lib/python3.10/site-packages/safedelete/query.py", line 66, in clone
    clone._safedelete_visibility = self._safedelete_visibility
AttributeError: 'SafeDeleteQuery' object has no attribute '_safedelete_visibility'
[01/Sep/2022 15:10:51] "GET /company/ HTTP/1.1" 500 133651

Seems that the SafeDeleteQuery object doesn’t always initialize the _safedelete_visibility and _safedelete_visibility_field attributes, which is giving the error in the clone() method. I’ve noticed the bug only triggers for specific queries, so it’s a bit hard to reproduce. I’ve made a sample Django project with a single endpoint that triggers the bug. I’ve included instructions to set up the project and reproduce the bug.

I’ve made a small PR that fixes the issue, it’s a simple guard check if the attributes are present or not. A cleaner solution I think would be to add sensical defaults, but I’m not sure if that would change any behavior, hence I went for a check instead.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Gagarocommented, Oct 3, 2022

I just released it in 1.3.1.

1reaction
roniemartinezcommented, Sep 27, 2022

We can confirm this works. Is there a final release date as we cannot see this yet in PyPI?

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found