title: How do I annotate a django queryset with StringAgg or ArrayAgg concatenating one column from mulitple children rows?
body: <p>Documents is the parent table.
  Paragraphs is the child table.</p>
  
  <p>Users filter Documents based on various search criteria.
  Then I wish to annotate Documents with certain Paragraphs filtered by a text query.
  The same text query is used to filter Documents and rank them (SearchRank). This ranking makes it necessary to start from Documents and annotate them with Paragraphs, instead of starting from Paragraphs and grouping them by Document.</p>
  
  <p>The postgresql way of concatenating one text field from multiple rows in Paragraphs would be the following:</p>
  
  <pre><code>SELECT array_to_string(
  ARRAY(
  SELECT paragraph.text
  FROM paragraph
  WHERE document id = '...'
  ORDER BY paragraph.number),
  ', ');
  </code></pre>
  
  <p>I am trying to translate this into django coding.</p>
  
  <p>I have tried numerous django approaches, to no avail.
  I can annotate 1 Paragraph.
  Query_sum is a Q() object built from user input.</p>
  
  <pre><code>results = Documents.filter(Query_sum)
  
  sub_paragraphs = Paragraphs.filter(Query_sum).filter(document=OuterRef('id'))
  
  results = results.annotate(paragraphs=Subquery(sub_paragraphs.values('text')[:1], output_field=TextField()))
  </code></pre>
  
  <p>Problems start when I get rid of slicing [:1].</p>
  
  <pre><code>results = results.annotate(paragraphs=Subquery(sub_paragraphs.values('text'), output_field=TextField()))
  </code></pre>
  
  <p>I then get the following error:
  "more than one row returned by a subquery used as an expression".</p>
  
  <p>To fix that, I tried to use ArrayAgg and StringAgg.
  I made quite a mess ;-)</p>
  
  <p>The Documents queryset (result) should be annotated either with a list of relevant Paragraphs (ArrayAgg), or a string of Paragraphs separated by any delimiter (StringAgg).</p>
  
  <p>Any idea of how to proceed? I would be extremely grateful</p>