diff options
| author | Jayantha Gumballi <jayantha.gumballi@gmail.com> | 2018-11-01 20:44:34 +0530 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-11-01 11:14:34 -0400 |
| commit | dfcdc8992f8bbbf072cefa4d325fe5cac5316f3d (patch) | |
| tree | de4a3cae1fafed36bdbfc7040d9349e0dd39dc16 | |
| parent | 74ddd0e83b039268e2988b777753cf01e417ccc1 (diff) | |
Fixed #29886 -- Fixed unaccent lookup when PostgreSQL's standard_conforming_strings option is off.
Thanks Tom McClure for the patch.
| -rw-r--r-- | django/db/backends/postgresql/base.py | 2 | ||||
| -rw-r--r-- | tests/postgres_tests/test_unaccent.py | 19 |
2 files changed, 20 insertions, 1 deletions
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index ad6c504261..9de9b44735 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -121,7 +121,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): # # Note: we use str.format() here for readability as '%' is used as a wildcard for # the LIKE operator. - pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')" + pattern_esc = r"REPLACE(REPLACE(REPLACE({}, E'\\', E'\\\\'), E'%%', E'\\%%'), E'_', E'\\_')" pattern_ops = { 'contains': "LIKE '%%' || {} || '%%'", 'icontains': "LIKE '%%' || UPPER({}) || '%%'", diff --git a/tests/postgres_tests/test_unaccent.py b/tests/postgres_tests/test_unaccent.py index 018aedb64c..477bb3d653 100644 --- a/tests/postgres_tests/test_unaccent.py +++ b/tests/postgres_tests/test_unaccent.py @@ -1,3 +1,4 @@ +from django.db import connection from django.test import modify_settings from . import PostgreSQLTestCase @@ -42,6 +43,24 @@ class UnaccentTest(PostgreSQLTestCase): ordered=False ) + def test_unaccent_with_conforming_strings_off(self): + """SQL is valid when standard_conforming_strings is off.""" + with connection.cursor() as cursor: + cursor.execute('SHOW standard_conforming_strings') + disable_conforming_strings = cursor.fetchall()[0][0] == 'on' + if disable_conforming_strings: + cursor.execute('SET standard_conforming_strings TO off') + try: + self.assertQuerysetEqual( + self.Model.objects.filter(field__unaccent__endswith='éÖ'), + ['àéÖ', 'aeO'], + transform=lambda instance: instance.field, + ordered=False, + ) + finally: + if disable_conforming_strings: + cursor.execute('SET standard_conforming_strings TO on') + def test_unaccent_accentuated_needle(self): self.assertQuerysetEqual( self.Model.objects.filter(field__unaccent="aéÖ"), |
