Opened 5 weeks ago

Closed 4 weeks ago

Last modified 4 weeks ago

#36111 closed Bug (fixed)

--debug-sql fails on Oracle if a query with params fails to execute and no prior query has either

Reported by: Jacob Walls Owned by: Jacob Walls
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Jacob Walls)

Noticed while working on ticket:35972 that an Oracle failure I wanted to debug with --debug-sql failed like:

======================================================================
ERROR: test_last_executed_query_without_previous_query (backends.tests.LastExecutedQueryTest)
last_executed_query should not raise an exception even if no previous
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/django/source/tests/backends/tests.py", line 80, in test_last_executed_query_without_previous_query
    connection.ops.last_executed_query(cursor, "SELECT %s;", (1,))
  File "/django/source/django/db/backends/oracle/operations.py", line 330, in last_executed_query
    statement = statement.replace(
AttributeError: 'NoneType' object has no attribute 'replace'

Failing test:

  • tests/backends/tests.py

    diff --git a/tests/backends/tests.py b/tests/backends/tests.py
    index 2adfa51360..b9cc4d119c 100644
    a b from django.db import ( (this hunk was shorter than expected)  
    1919from django.db.backends.base.base import BaseDatabaseWrapper
    2020from django.db.backends.signals import connection_created
    2121from django.db.backends.utils import CursorWrapper
    2222from django.db.models.sql.constants import CURSOR
    2323from django.test import (
    2424    TestCase,
    class LastExecutedQueryTest(TestCase):  
    7475        query has been run.
    7576        """
    7677        with connection.cursor() as cursor:
    77             connection.ops.last_executed_query(cursor, "", ())
     78            if connection.vendor == "oracle":
     79                cursor.statement = None
     80            connection.ops.last_executed_query(cursor, "SELECT %s;", (1,))
    7881
    7982    def test_debug_sql(self):
    8083        list(Reporter.objects.filter(first_name="test"))

The actual failure (where cursor.statement was None) happened via making this copy-paste mistake from the as_sql() method before it:

  • django/db/models/fields/json.py

    diff --git a/django/db/models/fields/json.py b/django/db/models/fields/json.py
    index d59c3afd71..108e4d7b0d 100644
    a b class HasKeyLookup(PostgresOperatorLookup):  
    252252            # queries but it is assumed that it cannot be evaded because the
    253253            # path is JSON serialized.
    254254            sql_parts.append(template % (lhs_sql, rhs_json_path))
    255             params.extend(lhs_params)
     255            params.extend(list(lhs_params) + [rhs_json_path])
    256256        return self._combine_sql_parts(sql_parts), tuple(params)
    257257
    258258    def as_postgresql(self, compiler, connection):

Change History (6)

comment:1 by Jacob Walls, 5 weeks ago

Has patch: set

comment:2 by Jacob Walls, 5 weeks ago

Description: modified (diff)

comment:3 by Tim Graham, 5 weeks ago

Triage Stage: UnreviewedAccepted

comment:4 by Mariusz Felisiak, 4 weeks ago

Triage Stage: AcceptedReady for checkin

comment:5 by GitHub <noreply@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In 330d89d:

Fixed #36111 -- Fixed test --debug-sql crash on Oracle when no prior query has executed.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 weeks ago

In e9576c0:

[5.2.x] Fixed #36111 -- Fixed test --debug-sql crash on Oracle when no prior query has executed.

Backport of 330d89d4fe7832355535580383523f1749a3ee45 from main

Note: See TracTickets for help on using tickets.
Back to Top