You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 29, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on May 29, 2026. It is now read-only.
If I have sqlcommenter configured for a Django project, including when running tests with the default Django test runner, the assertNumQueries assertion fails with the following exception:
Traceback (most recent call last):
File "test_something.py", line 4165, in test_some_feature
with self.assertNumQueries(5):
~~~~~~~~~~~~~~~~~~~~~^^^
File ".venv/lib/python3.13/site-packages/django/test/testcases.py", line 135, in __exit__
"\n".join(
~~~~~~~~~^
"%d. %s" % (i, query["sql"])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
for i, query in enumerate(self.captured_queries, start=1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
),
^
File ".venv/lib/python3.13/site-packages/django/test/testcases.py", line 136, in <genexpr>
"%d. %s" % (i, query["sql"])
~~~~~^^^^^^^
TypeError: string indices must be integers, not 'str'
Django version 5.1.4, Python 3.13.1, sqlcommenter 2.0.0.
Even if the type is fixed, the queries will be added to the queries_log twice (one entry added before execution, one after), which doesn't work nicely with, e.g., assertNumQueries from Django.
Activity
florianschieder commentedon Mar 7, 2025
+1 same for me on django 5.0.11 and sqlcommenter 2.0.0
i assume the error cause to be there in just appending the plain sql string to the connection query log instead of passing a dictionary like
{"sql": QUERY_STRING_HERE,# possible further stuff django expects}https://github.com/google/sqlcommenter/blob/master/python/sqlcommenter-python/google/cloud/sqlcommenter/django/middleware.py#L94C1-L95C58
going to take a look at this next week and will maybe provide a pull request if I find time
artamonovkirill commentedon Jan 8, 2026
CursorDebugWrapperalready adds an entry to thequeries_logfor the query post-execution (with atimekey): https://github.com/django/django/blob/main/django/db/backends/utils.py#L145-L150Even if the type is fixed, the queries will be added to the
queries_logtwice (one entry added before execution, one after), which doesn't work nicely with, e.g.,assertNumQueriesfrom Django.