﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36571	Deprecated usage of BINARY expr in MySQL lookups	Simon Charette	Youssef Tarek Ali	"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""
}}}"	Cleanup/optimization	assigned	Database layer (models, ORM)	dev	Normal		mysql binary like		Accepted	1	0	0	1	0	0
