[Django] #36111: --debug-sql fails on Oracle if a query with params fails to execute and no prior query has either

23 views
Skip to first unread message

Django

unread,
Jan 18, 2025, 2:21:22 PM1/18/25
to django-...@googlegroups.com
#36111: --debug-sql fails on Oracle if a query with params fails to execute and no
prior query has either
---------------------------------------------+-----------------------------
Reporter: Jacob Walls | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
---------------------------------------------+-----------------------------
Noticed while working on ticket:35972 that an Oracle failure I wanted to
debug with `--debug-sql` failed like:

{{{#!py
======================================================================
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;", (Value(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:

{{{#!diff
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index 2adfa51360..b9cc4d119c 100644
--- a/tests/backends/tests.py
+++ b/tests/backends/tests.py
@@ -19,6 +19,7 @@ from django.db import (
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.signals import connection_created
from django.db.backends.utils import CursorWrapper
+from django.db.models import Value
from django.db.models.sql.constants import CURSOR
from django.test import (
TestCase,
@@ -74,7 +75,9 @@ class LastExecutedQueryTest(TestCase):
query has been run.
"""
with connection.cursor() as cursor:
- connection.ops.last_executed_query(cursor, "", ())
+ if connection.vendor == "oracle":
+ cursor.statement = None
+ connection.ops.last_executed_query(cursor, "SELECT %s;",
(Value(1),))

def test_debug_sql(self):
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:
{{{#!diff
diff --git a/django/db/models/fields/json.py
b/django/db/models/fields/json.py
index d59c3afd71..108e4d7b0d 100644
--- a/django/db/models/fields/json.py
+++ b/django/db/models/fields/json.py
@@ -252,7 +252,7 @@ class HasKeyLookup(PostgresOperatorLookup):
# queries but it is assumed that it cannot be evaded because
the
# path is JSON serialized.
sql_parts.append(template % (lhs_sql, rhs_json_path))
- params.extend(lhs_params)
+ params.extend(list(lhs_params) + [rhs_json_path])
return self._combine_sql_parts(sql_parts), tuple(params)

def as_postgresql(self, compiler, connection):
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36111>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 18, 2025, 2:31:20 PM1/18/25
to django-...@googlegroups.com
#36111: --debug-sql fails on Oracle if a query with params fails to execute and no
prior query has either
-----------------------------------+---------------------------------------
Reporter: Jacob Walls | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+---------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/19069 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36111#comment:1>

Django

unread,
Jan 18, 2025, 2:40:47 PM1/18/25
to django-...@googlegroups.com
#36111: --debug-sql fails on Oracle if a query with params fails to execute and no
prior query has either
-----------------------------------+---------------------------------------
Reporter: Jacob Walls | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+---------------------------------------
Description changed by Jacob Walls:

Old description:
New description:

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

{{{#!py
======================================================================
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:

{{{#!diff
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index 2adfa51360..b9cc4d119c 100644
--- a/tests/backends/tests.py
+++ b/tests/backends/tests.py
@@ -19,6 +19,7 @@ from django.db import (
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.signals import connection_created
from django.db.backends.utils import CursorWrapper
from django.db.models.sql.constants import CURSOR
from django.test import (
TestCase,
@@ -74,7 +75,9 @@ class LastExecutedQueryTest(TestCase):
query has been run.
"""
with connection.cursor() as cursor:
- connection.ops.last_executed_query(cursor, "", ())
+ if connection.vendor == "oracle":
+ cursor.statement = None
+ connection.ops.last_executed_query(cursor, "SELECT %s;",
(1,))

def test_debug_sql(self):
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:
{{{#!diff
diff --git a/django/db/models/fields/json.py
b/django/db/models/fields/json.py
index d59c3afd71..108e4d7b0d 100644
--- a/django/db/models/fields/json.py
+++ b/django/db/models/fields/json.py
@@ -252,7 +252,7 @@ class HasKeyLookup(PostgresOperatorLookup):
# queries but it is assumed that it cannot be evaded because
the
# path is JSON serialized.
sql_parts.append(template % (lhs_sql, rhs_json_path))
- params.extend(lhs_params)
+ params.extend(list(lhs_params) + [rhs_json_path])
return self._combine_sql_parts(sql_parts), tuple(params)

def as_postgresql(self, compiler, connection):
}}}

--
--
Ticket URL: <https://code.djangoproject.com/ticket/36111#comment:2>

Django

unread,
Jan 19, 2025, 7:51:17 PM1/19/25
to django-...@googlegroups.com
#36111: --debug-sql fails on Oracle if a query with params fails to execute and no
prior query has either
-----------------------------------+---------------------------------------
Reporter: Jacob Walls | Owner: Jacob Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------------+---------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36111#comment:3>

Django

unread,
Jan 25, 2025, 12:05:40 PM1/25/25
to django-...@googlegroups.com
#36111: --debug-sql fails on Oracle if a query with params fails to execute and no
prior query has either
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36111#comment:4>

Django

unread,
Jan 25, 2025, 12:06:21 PM1/25/25
to django-...@googlegroups.com
#36111: --debug-sql fails on Oracle if a query with params fails to execute and no
prior query has either
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: closed
Component: Testing framework | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"330d89d4fe7832355535580383523f1749a3ee45" 330d89d]:
{{{#!CommitTicketReference repository=""
revision="330d89d4fe7832355535580383523f1749a3ee45"
Fixed #36111 -- Fixed test --debug-sql crash on Oracle when no prior query
has executed.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36111#comment:5>

Django

unread,
Jan 25, 2025, 12:07:55 PM1/25/25
to django-...@googlegroups.com
#36111: --debug-sql fails on Oracle if a query with params fails to execute and no
prior query has either
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: closed
Component: Testing framework | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"e9576c0aa86186e25b92c3c4f6b1bd7f20799db9" e9576c0]:
{{{#!CommitTicketReference repository=""
revision="e9576c0aa86186e25b92c3c4f6b1bd7f20799db9"
[5.2.x] Fixed #36111 -- Fixed test --debug-sql crash on Oracle when no
prior query has executed.

Backport of 330d89d4fe7832355535580383523f1749a3ee45 from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36111#comment:6>
Reply all
Reply to author
Forward
0 new messages