#36571: Deprecated usage of BINARY expr in MySQL lookups
-------------------------------------+-------------------------------------
Reporter: Simon Charette | Type:
| Cleanup/optimization
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: mysql binary like | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
When enabling warnings against MySQL usage of `__contains`,
`__startswith`, `__endswith` on MySQL result in the following warning
> Warning 1287 "'BINARY expr' is deprecated and will be removed in a
future release
It seems like a potential solution is simply to use `CAST` instead
{{{#!diff
diff --git a/django/db/backends/mysql/base.py
b/django/db/backends/mysql/base.py
index e83dc106f7..236e6c599e 100644
--- a/django/db/backends/mysql/base.py
+++ b/django/db/backends/mysql/base.py
@@ -164,14 +164,14 @@ def data_types(self):
operators = {
"exact": "= %s",
"iexact": "LIKE %s",
- "contains": "LIKE BINARY %s",
+ "contains": "LIKE CAST(%s AS BINARY)",
"icontains": "LIKE %s",
"gt": "> %s",
"gte": ">= %s",
"lt": "< %s",
"lte": "<= %s",
- "startswith": "LIKE BINARY %s",
- "endswith": "LIKE BINARY %s",
+ "startswith": "LIKE CAST(%s AS BINARY)",
+ "endswith": "LIKE CAST(%s AS BINARY)",
"istartswith": "LIKE %s",
"iendswith": "LIKE %s",
}
@@ -186,11 +186,11 @@ def data_types(self):
# wildcard for the LIKE operator.
pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\\', '\\\\'), '%%',
'\%%'), '_', '\_')"
pattern_ops = {
- "contains": "LIKE BINARY CONCAT('%%', {}, '%%')",
+ "contains": "LIKE CAST(CONCAT('%%', {}, '%%') AS BINARY)",
"icontains": "LIKE CONCAT('%%', {}, '%%')",
- "startswith": "LIKE BINARY CONCAT({}, '%%')",
+ "startswith": "LIKE CAST(CONCAT({}, '%%') AS BINARY)",
"istartswith": "LIKE CONCAT({}, '%%')",
- "endswith": "LIKE BINARY CONCAT('%%', {})",
+ "endswith": "LIKE CAST(CONCAT('%%', {}) AS BINARY)",
"iendswith": "LIKE CONCAT('%%', {})",
}
diff --git a/django/db/backends/mysql/operations.py
b/django/db/backends/mysql/operations.py
index 7dfcd57958..bdde5bbf47 100644
--- a/django/db/backends/mysql/operations.py
+++ b/django/db/backends/mysql/operations.py
@@ -363,7 +363,7 @@ def regex_lookup(self, lookup_type):
# REGEXP_LIKE doesn't exist in MariaDB.
if self.connection.mysql_is_mariadb:
if lookup_type == "regex":
- return "%s REGEXP BINARY %s"
+ return "%s REGEXP CAST(%s AS BINARY)"
return "%s REGEXP %s"
match_option = "c" if lookup_type == "regex" else "i"
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/36571>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.