Skip to content

Commit 1bbcc91

Browse files
tukyadamchainz
authored andcommitted
Fix GroupConcat to put ORDER BY before SEPARATOR (#596)
Otherwise you end up with a syntax error if you set both, `separator` and `ordering`! https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
1 parent df59aca commit 1bbcc91

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Pending
1515
``importlib.metadata.version("django-mysql")``
1616
(`docs <https://docs.python.org/3.8/library/importlib.metadata.html#distribution-versions>`__ /
1717
`backport <https://pypi.org/project/importlib-metadata/>`__).
18+
* Fix ``GroupConcat`` to work with both ``separator`` and ``ordering`` set.
19+
(`PR #596 <https://github.com/adamchainz/django-mysql/pull/596>`__).
1820

1921
3.2.0 (2019-06-14)
2022
------------------

src/django_mysql/models/aggregates.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ def as_sql(self, compiler, connection, function=None, template=None):
5252

5353
sql.append(expr_sql)
5454

55-
if self.separator is not None:
56-
sql.append(" SEPARATOR '{}'".format(self.separator))
57-
5855
if self.ordering is not None:
5956
sql.append(" ORDER BY ")
6057
sql.append(expr_sql)
6158
params.extend(params[:])
6259
sql.append(" ")
6360
sql.append(self.ordering.upper())
6461

62+
if self.separator is not None:
63+
sql.append(" SEPARATOR '{}'".format(self.separator))
64+
6565
sql.append(")")
6666

6767
return "".join(sql), tuple(params)

tests/testapp/test_aggregates.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,9 @@ def test_ordering_asc(self):
132132
def test_ordering_desc(self):
133133
out = self.shakes.tutees.aggregate(tids=GroupConcat("id", ordering="desc"))
134134
assert out == {"tids": ",".join(reversed(self.str_tutee_ids))}
135+
136+
def test_separator_ordering(self):
137+
concat = GroupConcat("id", separator=":", ordering="asc")
138+
out = self.shakes.tutees.aggregate(tids=concat)
139+
concatted_ids = ":".join(self.str_tutee_ids)
140+
assert out == {"tids": concatted_ids}

0 commit comments

Comments
 (0)