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.

When using DjangoDebugMiddleware, debug tracking presumes cursor.execute argument is a string, raising an error

See original GitHub issue
  • What is the current behavior?

When using the DjangoDebugMiddleware, Graphene Django wraps cursor.execute calls in a class that logs the query. Among the properties it logs is is_select, which attempts to guess if a query is a select query by looking for “select” in the argument to cursor.execute:

                "is_select": sql.lower().strip().startswith("select"),

When cursor.execute is called with a non-string argument, such as a psycopg2 query template, this raises an error:

from django.db import connection
from psycopg2 import sql

with connection.cursor() as cursor:
    query = sql.SQL("SELECT * FROM {table}").format(table=sql.Identifier("my_table"))
    cursor.execute(query)

This raises:

graphql.error.located_error.GraphQLLocatedError: 'Composed' object has no attribute 'lower'
  • What is the expected behavior? Grahene Django should not presume that the argument to connection.cursor() will always be a string, and do appropriate string coercion as needed before calling string methods on it.

  • What is the motivation / use case for changing the behavior? Robustness. It’s ideal not to throw exceptions from normal uses of core Django methods which graphene-django monkeypatches.

  • Please tell us about your environment:

    • Version: graphene-django 2.5.0
    • Platform: Ubuntu Linux 18.04, Django 2.2.12, graphene 2.1.8

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mtully-blitzcommented, Jun 23, 2020

Ran into the same issue. A possibly naive solution from playing around in my debugger. It fixed the issue in my case, but unsure of the side effects beyond the scope of this function.

https://github.com/graphql-python/graphene-django/blob/master/graphene_django/debug/sql/tracking.py#L115

sql_string = self.db.ops.last_executed_query(
        self.cursor, sql, self._quote_params(params)
    )
params = {
    "vendor": vendor,
    "alias": alias,
    "sql": sql_string,
    "duration": duration,
    "raw_sql": sql,
    "params": _params,
    "start_time": start_time,
    "stop_time": stop_time,
    "is_slow": duration > 10,
    "is_select": sql_string.lower().strip().startswith("select"),
}
0reactions
zbyte64commented, Oct 26, 2020

I like @mtully-blitz solution, doesn’t appear incorrect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python, mysql, inserting string into table, error 1054
Use parametrized SQL: cursor.execute("INSERT INTO User(user_id,username,user) VALUES (%s,%s,%s)", (a,b,c)). (Notice the values (a,b,c) are passed to the ...
Read more >
Understanding the Python Traceback
Python prints a traceback when an exception is raised in your code. The traceback output can be a ... Here, greet() gets called...
Read more >
Performing raw SQL queries | Django documentation
This method takes a raw SQL query, executes it, and returns a django.db.models.query.RawQuerySet instance. This RawQuerySet instance can be iterated over ...
Read more >
10.12.2 errors.Error Exception - MySQL :: Developer Zone
Error is internally used by Connector/Python to raise MySQL client and server errors ... The following examples show the result when using no...
Read more >
Frequently Asked Questions - Psycopg 2.8.6 documentation
Problems with type conversions. Why does cursor.execute() raise the exception can't adapt ? Psycopg converts Python objects in a SQL string representation ...
Read more >

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