| 1 | diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
|
|---|
| 2 | index ad6c504261..9de9b44735 100644
|
|---|
| 3 | --- a/django/db/backends/postgresql/base.py
|
|---|
| 4 | +++ b/django/db/backends/postgresql/base.py
|
|---|
| 5 | @@ -121,7 +121,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
|---|
| 6 | #
|
|---|
| 7 | # Note: we use str.format() here for readability as '%' is used as a wildcard for
|
|---|
| 8 | # the LIKE operator.
|
|---|
| 9 | - pattern_esc = r"REPLACE(REPLACE(REPLACE({}, '\', '\\'), '%%', '\%%'), '_', '\_')"
|
|---|
| 10 | + pattern_esc = r"REPLACE(REPLACE(REPLACE({}, E'\\', E'\\\\'), E'%%', E'\\%%'), E'_', E'\\_')"
|
|---|
| 11 | pattern_ops = {
|
|---|
| 12 | 'contains': "LIKE '%%' || {} || '%%'",
|
|---|
| 13 | 'icontains': "LIKE '%%' || UPPER({}) || '%%'",
|
|---|
| 14 | diff --git a/tests/postgres_tests/test_unaccent.py b/tests/postgres_tests/test_unaccent.py
|
|---|
| 15 | index 018aedb64c..ae7027713c 100644
|
|---|
| 16 | --- a/tests/postgres_tests/test_unaccent.py
|
|---|
| 17 | +++ b/tests/postgres_tests/test_unaccent.py
|
|---|
| 18 | @@ -42,6 +42,28 @@ class UnaccentTest(PostgreSQLTestCase):
|
|---|
| 19 | ordered=False
|
|---|
| 20 | )
|
|---|
| 21 |
|
|---|
| 22 | + def test_unaccent_chained_with_conforming_strings_off(self):
|
|---|
| 23 | + """
|
|---|
| 24 | + Chained unaccent causes the comparison string to be escaped.
|
|---|
| 25 | + Make sure the escaping SQL is valid even if the DB has standard_conforming_strings OFF.
|
|---|
| 26 | + """
|
|---|
| 27 | + from django.db import connection
|
|---|
| 28 | + cur = connection.cursor()
|
|---|
| 29 | + cur.execute("SET standard_conforming_strings TO OFF;")
|
|---|
| 30 | + self.assertQuerysetEqual(
|
|---|
| 31 | + self.Model.objects.filter(field__unaccent__iexact="aeO"),
|
|---|
| 32 | + ["àéÖ", "aeO", "aeo"],
|
|---|
| 33 | + transform=lambda instance: instance.field,
|
|---|
| 34 | + ordered=False
|
|---|
| 35 | + )
|
|---|
| 36 | + self.assertQuerysetEqual(
|
|---|
| 37 | + self.Model.objects.filter(field__unaccent__endswith="éÖ"),
|
|---|
| 38 | + ["àéÖ", "aeO"],
|
|---|
| 39 | + transform=lambda instance: instance.field,
|
|---|
| 40 | + ordered=False
|
|---|
| 41 | + )
|
|---|
| 42 | + cur.execute("SET standard_conforming_strings TO ON;")
|
|---|
| 43 | +
|
|---|
| 44 | def test_unaccent_accentuated_needle(self):
|
|---|
| 45 | self.assertQuerysetEqual(
|
|---|
| 46 | self.Model.objects.filter(field__unaccent="aéÖ"),
|
|---|