Skip to content

Commit 3d5f478

Browse files
authored
Merge pull request #630 from squareapartments/master
Add check if function is aggregate in returning
2 parents 920a4cd + f2adfe0 commit 3d5f478

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pypika/dialects.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,10 @@ def returning(self, *terms: Any) -> "PostgreSQLQueryBuilder":
557557
self._return_field(term)
558558
elif isinstance(term, str):
559559
self._return_field_str(term)
560-
elif isinstance(term, ArithmeticExpression):
560+
elif isinstance(term, (Function, ArithmeticExpression)):
561+
if term.is_aggregate:
562+
raise QueryException("Aggregate functions are not allowed in returning")
561563
self._return_other(term)
562-
elif isinstance(term, Function):
563-
raise QueryException("Aggregate functions are not allowed in returning")
564564
else:
565565
self._return_other(self.wrap_constant(term, self._wrapper_cls))
566566

pypika/tests/test_inserts.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
Tables,
1212
functions as fn,
1313
)
14-
from pypika.functions import Avg
14+
from pypika.functions import (
15+
Avg,
16+
Cast,
17+
)
1518
from pypika.terms import Values
1619
from pypika.utils import QueryException
1720

@@ -550,6 +553,11 @@ def test_insert_returning_arithmetics(self):
550553

551554
self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING "f1"+"f2"', str(query))
552555

556+
def test_insert_returning_functions(self):
557+
query = PostgreSQLQuery.into(self.table_abc).insert(1).returning(Cast(self.table_abc.f1, "int"))
558+
559+
self.assertEqual('INSERT INTO "abc" VALUES (1) RETURNING CAST("f1" AS INT)', str(query))
560+
553561
def test_insert_returning_aggregate(self):
554562
with self.assertRaises(QueryException):
555563
PostgreSQLQuery.into(self.table_abc).insert(1).returning(Avg(self.table_abc.views))

0 commit comments

Comments
 (0)