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

Running queryset.explain will result in an error #597

Open
mounirmesselmeni opened this issue Sep 16, 2022 · 0 comments · May be fixed by #654
Open

Running queryset.explain will result in an error #597

mounirmesselmeni opened this issue Sep 16, 2022 · 0 comments · May be fixed by #654

Comments

@mounirmesselmeni
Copy link

mounirmesselmeni commented Sep 16, 2022

Using djangoql as example which does run queryset.explain will result in a ProgrammingError.
Reproducing would be easy by adding a queryset.explain in a view or installing djangoql and try to preform a search in the Django admin.

The generated SQL will contains two explains

syntax error at or near "EXPLAIN"
LINE 1: EXPLAIN EXPLAIN SELECT ...

This is caused by _explain_query and exactly this part

        # currently we cannot use explain() method
        # for queries other than `select`
        prefixed_query = f"{prefix} {q}"
        with connection.cursor() as cur:
            cur.execute(prefixed_query, params)
            result = _unpack_explanation(cur.fetchall())
            return '\n'.join(result)

A possible way to fix this is to check if the SQL starts with explain and skip the check/analyze.

        # currently we cannot use explain() method
        # for queries other than `select`
        # Skip explain queries
        if q.lower().startswith('explain'):
            return None
        prefixed_query = f"{prefix} {q}"
        with connection.cursor() as cur:
            cur.execute(prefixed_query, params)
            result = _unpack_explanation(cur.fetchall())
            return '\n'.join(result)

If that's fine I can prepare a PR for this.

Thank you.

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

Successfully merging a pull request may close this issue.

2 participants