Fixed #34955 -- Made Concat() use || operator on PostgreSQL.

This also avoids casting string based expressions in Concat() on
PostgreSQL.
This commit is contained in:
Simon Charette 2023-11-14 09:30:14 +01:00 committed by Mariusz Felisiak
parent bdf30b952c
commit 6364b6ee10
2 changed files with 37 additions and 15 deletions

View file

@ -75,7 +75,10 @@ class ConcatTests(TestCase):
expected = article.title + " - " + article.text
self.assertEqual(expected.upper(), article.title_text)
@skipUnless(connection.vendor == "sqlite", "sqlite specific implementation detail.")
@skipUnless(
connection.vendor in ("sqlite", "postgresql"),
"SQLite and PostgreSQL specific implementation detail.",
)
def test_coalesce_idempotent(self):
pair = ConcatPair(V("a"), V("b"))
# Check nodes counts
@ -89,3 +92,18 @@ class ConcatTests(TestCase):
qs = Article.objects.annotate(description=Concat("title", V(": "), "summary"))
# Multiple compilations should not alter the generated query.
self.assertEqual(str(qs.query), str(qs.all().query))
def test_concat_non_str(self):
Author.objects.create(name="The Name", age=42)
with self.assertNumQueries(1) as ctx:
author = Author.objects.annotate(
name_text=Concat(
"name", V(":"), "alias", V(":"), "age", output_field=TextField()
),
).get()
self.assertEqual(author.name_text, "The Name::42")
# Only non-string columns are casted on PostgreSQL.
self.assertEqual(
ctx.captured_queries[0]["sql"].count("::text"),
1 if connection.vendor == "postgresql" else 0,
)