diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index ea6f50ef1c7..91e0e832420 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -141,8 +141,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): 'iexact': 'LIKE %s', 'contains': 'LIKE BINARY %s', 'icontains': 'LIKE %s', - 'regex': 'REGEXP BINARY %s', - 'iregex': 'REGEXP %s', 'gt': '> %s', 'gte': '>= %s', 'lt': '< %s', diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index e5c0c5c71b7..47e179db41e 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -282,3 +282,14 @@ class DatabaseOperations(BaseDatabaseOperations): # EXTENDED and FORMAT are mutually exclusive options. prefix += ' EXTENDED' return prefix + + def regex_lookup(self, lookup_type): + # REGEXP BINARY doesn't work correctly in MySQL 8+ and REGEXP_LIKE + # doesn't exist in MySQL 5.6. + if self.connection.mysql_version < (8, 0, 0): + if lookup_type == 'regex': + return '%s REGEXP BINARY %s' + return '%s REGEXP %s' + + match_option = 'c' if lookup_type == 'regex' else 'i' + return "REGEXP_LIKE(%%s, %%s, '%s')" % match_option diff --git a/docs/releases/2.0.7.txt b/docs/releases/2.0.7.txt index 4890ee2dbab..8d06deb226f 100644 --- a/docs/releases/2.0.7.txt +++ b/docs/releases/2.0.7.txt @@ -14,3 +14,5 @@ Bugfixes * Fixed admin check crash when using a query expression in ``ModelAdmin.ordering`` (:ticket:`29428`). + +* Fixed ``__regex`` and ``__iregex`` lookups with MySQL 8 (:ticket:`29451`).