#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070198e1dfd7c9-1cb943bf-b78c-45b7-b9f4-5a3726799d90-000000%40eu-central-1.amazonses.com.

Reply via email to