>>>from example.models import Example
>>> Example.objects.create(field='foo')
<Example: Example object (1)>>>> Example.objects.create(field='bar')
<Example: Example object (2)>>>> Example.objects.create(field='test')
<Example: Example object (3)>
>>> Example.objects.aggregate(result=StringAgg('field', delimiter="'"))
Traceback (most recent call last):
File "/Users/ryu22e/develompent/temp/cve_2020_7471/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.SyntaxError: unterminated quoted string at or near "''') AS "result" FROM "example_example""LINE 1: SELECT STRING_AGG("example_example"."field", ''') AS "result...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
(中略)
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: unterminated quoted string at or near "''') AS "result" FROM "example_example""LINE 1: SELECT STRING_AGG("example_example"."field", ''') AS "result...
実際に生成されるクエリの全体像も見てみましょう。
1
2
>>> q = Example.objects.annotate(result=StringAgg('field', delimiter="'")).query; str(q)
'SELECT "example_example"."id", "example_example"."field", STRING_AGG("example_example"."field", \'\'\') AS "result" FROM "example_example" GROUP BY "example_example"."id"'