Skip to content

Commit a41b092

Browse files
authored
Fixed #30380 -- Handled bytes in MySQL backend for PyMySQL support.
This commit partly reverts efd8a82.
1 parent 12b7956 commit a41b092

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

django/db/backends/mysql/operations.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.db.backends.base.operations import BaseDatabaseOperations
55
from django.utils import timezone
66
from django.utils.duration import duration_microseconds
7+
from django.utils.encoding import force_str
78

89

910
class DatabaseOperations(BaseDatabaseOperations):
@@ -141,10 +142,8 @@ def last_executed_query(self, cursor, sql, params):
141142
# With MySQLdb, cursor objects have an (undocumented) "_executed"
142143
# attribute where the exact query sent to the database is saved.
143144
# See MySQLdb/cursors.py in the source distribution.
144-
query = getattr(cursor, '_executed', None)
145-
if query is not None:
146-
query = query.decode(errors='replace')
147-
return query
145+
# MySQLdb returns string, PyMySQL bytes.
146+
return force_str(getattr(cursor, '_last_executed', None), errors='replace')
148147

149148
def no_limit_value(self):
150149
# 2**64 - 1, as recommended by the MySQL documentation

django/db/backends/mysql/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
3131

3232
def quote_value(self, value):
3333
self.connection.ensure_connection()
34+
# MySQLdb escapes to string, PyMySQL to bytes.
3435
quoted = self.connection.connection.escape(value, self.connection.connection.encoders)
35-
if isinstance(value, str):
36+
if isinstance(value, str) and isinstance(quoted, bytes):
3637
quoted = quoted.decode()
3738
return quoted
3839

0 commit comments

Comments
 (0)