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

explain_format missing in self.query causes django admin app to not work properly #409

Open
evan54 opened this issue Aug 29, 2024 · 3 comments

Comments

@evan54
Copy link

evan54 commented Aug 29, 2024

I came across it when trying to use the admin page on the django app and it always errors out when trying to use DjangoQL as that seems to use this explain function and errors out so you can't actually search for entires in the admin app.

Software versions

  • Django: 4.2.15
  • mssql-django: 1.5
  • python: 3.10.11
  • SQL Server: 17
  • OS: Windows

models.py

from django.db import models

class MyModel(models.Model):
    name = models.CharField(blank=False, max_length=255)
    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=['name'],
                condition=models.Q(name__isnull=False),
                name='non_null_name_unique',
            ),
        ]

Database Connection Settings
DATABASES = { 'default': { 'ENGINE': 'mssql', 'HOST': my_host, 'NAME': my_name, 'OPTIONS': { 'driver': 'ODBC Driver 17 for SQL Server', 'host_is_server': True, } } }

Problem description and steps to reproduce

ensure you have some entreis in MyModel in

python manage.py shell  # enter shell
from main.models import m
qs = m.MyModel.objects.all()  # or any code to load some objects in a queryset
qs.explain()  # this errors out

Expected behavior and actual behavior
qs.explain() shouldn't error out - should get smth like a string with linebreaks as outputs it seems like?

Error message/stack trace

Traceback (most recent call last):
File "C:\Users\UserPath.venv\django\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\contrib\admin\options.py", line 688, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\utils\decorators.py", line 134, in _wrapper_view
response = view_func(request, *args, **kwargs)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\views\decorators\cache.py", line 62, in _wrapper_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\contrib\admin\sites.py", line 242, in inner
return view(request, *args, **kwargs)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\utils\decorators.py", line 134, in _wrapper_view
response = view_func(request, *args, **kwargs)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\contrib\admin\options.py", line 1926, in changelist_view
cl = self.get_changelist_instance(request)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\contrib\admin\options.py", line 836, in get_changelist_instance
return ChangeList(
File "C:\Users\UserPath.venv\django\lib\site-packages\django\contrib\admin\views\main.py", line 122, in init
self.queryset = self.get_queryset(request)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\contrib\admin\views\main.py", line 529, in get_queryset
qs, search_may_have_duplicates = self.model_admin.get_search_results(
File "C:\Users\UserPath.venv\django\lib\site-packages\djangoql\admin.py", line 93, in get_search_results
explain()
File "C:\Users\UserPath.venv\django\lib\site-packages\django\db\models\query.py", line 1281, in explain
return self.query.explain(using=self.db, format=format, **options)
File "C:\Users\UserPath.venv\django\lib\site-packages\django\db\models\sql\query.py", line 610, in explain
return "\n".join(compiler.explain_query())
File "C:\Users\UserPath.venv\django\lib\site-packages\django\db\models\sql\compiler.py", line 1611, in explain_query
result = list(self.execute_sql())
File "C:\Users\UserPath.venv\django\lib\site-packages\django\db\models\sql\compiler.py", line 1549, in execute_sql
sql, params = self.as_sql()
File "C:\Users\UserPath.venv\django\lib\site-packages\mssql\compiler.py", line 366, in as_sql
self.query.explain_format,

Exception Type: AttributeError at /admin/main/mymodel/
Exception Value: 'Query' object has no attribute 'explain_format'

Any other details that can be helpful
I think I was able to fix it by editing line ln 364 (https://github.com/microsoft/mssql-django/blob/dev/mssql/compiler.py#L364) and doing if explain and hasattr(self.query, 'explain_format'): so it only enters if that is available.

Found this which seems to have some similar issues but it's quite old https://github.com/ESSolutions/django-mssql-backend/pull/126/files

@mShan0
Copy link
Contributor

mShan0 commented Aug 29, 2024

Hi @evan54, thanks for reporting this. We will get this fix merged in for the next release

@evan54
Copy link
Author

evan54 commented Oct 13, 2024

I saw that this failed some checks - not sure how that works but saw it here #410.

I found the below which seems to be more sensible - ie adapts to the new version of django vs in a hacky way checking if the code will run or not.

https://github.com/dimagi/django-cte/blob/master/django_cte/query.py#L120

This is how I adapted it

if django.VERSION >= (4, 0):
    explain_attribute = "explain_info"
    explain_info = getattr(self.query, explain_attribute, None)
    explain_format = getattr(explain_info, "format", None)
    explain_options = getattr(explain_info, "options", {})
else:
    explain_attribute = "explain_query"
    explain_format = getattr(self.query, "explain_format", None)
    explain_options = getattr(self.query, "explain_options", {})

explain_query_or_info = getattr(self.query, explain_attribute, None)
if explain_query_or_info:
    result.insert(0, self.connection.ops.explain_query_prefix(
        explain_format, **explain_options
    ))

However this still doesn't work I now get an error sayhing django.db.utils.NotSupportedError: This backend does not support explaining query execution. is this something related to my particular version of SQL Server or that SQL server generally doesn't support this? If it doesn't then why bother with this piece of code?

@SaschaKrug
Copy link

@mShan0, are there plans to fix this issue? Right now mssql-django doesn't support Django 4.0+ fully due to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants