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..ccc1c53878 100644
|
---|
16 | --- a/tests/postgres_tests/test_unaccent.py
|
---|
17 | +++ b/tests/postgres_tests/test_unaccent.py
|
---|
18 | @@ -1,3 +1,4 @@
|
---|
19 | +from django.db import connection, DatabaseError
|
---|
20 | from django.test import modify_settings
|
---|
21 |
|
---|
22 | from . import PostgreSQLTestCase
|
---|
23 | @@ -42,6 +43,24 @@ class UnaccentTest(PostgreSQLTestCase):
|
---|
24 | ordered=False
|
---|
25 | )
|
---|
26 |
|
---|
27 | + def test_unaccent_chained_with_conforming_strings_off(self):
|
---|
28 | + """
|
---|
29 | + Chained unaccent causes the comparison string to be escaped.
|
---|
30 | + Make sure the escaping SQL is valid even if the DB has standard_conforming_strings OFF.
|
---|
31 | + """
|
---|
32 | + with connection.cursor() as cur:
|
---|
33 | + cur.execute("SET standard_conforming_strings TO OFF;")
|
---|
34 | + try:
|
---|
35 | + self.assertQuerysetEqual(
|
---|
36 | + self.Model.objects.filter(field__unaccent__endswith="éÖ"),
|
---|
37 | + ["àéÖ", "aeO"],
|
---|
38 | + transform=lambda instance: instance.field,
|
---|
39 | + ordered=False
|
---|
40 | + )
|
---|
41 | + except DatabaseError:
|
---|
42 | + self.fail("DatabaseError should not have occurred.")
|
---|
43 | + cur.execute("SET standard_conforming_strings TO ON;")
|
---|
44 | +
|
---|
45 | def test_unaccent_accentuated_needle(self):
|
---|
46 | self.assertQuerysetEqual(
|
---|
47 | self.Model.objects.filter(field__unaccent="aéÖ"),
|
---|