Hi Simon, thank you for your suggestion!
I managed to accomplish that for one column with custom ROW function and 
JSON version with JSON_AGG and JSON_BUILD_OBJECT.

However when using two such annotations the number of aggregated elements 
gets multiplied by the number of elements in the other relation.
I found a different way to get such aggregations independently: 
http://dba.stackexchange.com/a/130212/88817
but when I use extra(select={...}) the same SQL code is placed in GROUP BY 
clause, which results in:

ProgrammingError: could not identify an equality operator for type json

How to prevent adding this extra select to GROUP BY? Without it the query 
works well.

Best wishes
Tomasz


W dniu wtorek, 1 marca 2016 03:52:58 UTC+1 użytkownik Simon Charette 
napisał:
>
> Hi Tomasz,
>
> I suppose you could implement `ROW` as a custom expression[1] and use it in
> your `ArrayAgg` aggregate as follow:
>
> from django.contrib.postgres.aggregates import ArrayAgg
> from django.db.models import Expression
>
> class Row(Expression):
>     template = 'ROW(%(expressions)s)'
>
>     # ...
>
> MyModel.objects.aggregate(
>     ArrayAgg(Row(F('field1'), F('field2'))),
> )
>
>
> Cheers,
> Simon
>
> [1] 
> https://docs.djangoproject.com/en/1.9/ref/models/expressions/#writing-your-own-query-expressions
>
> Le lundi 29 février 2016 19:31:15 UTC-5, Tomasz Nowak a écrit :
>>
>> Hi!
>>
>> Module django.contrib.postgres.aggregates provides classes for ARRAY_AGG 
>> and STRING_AGG PostgreSQL aggregates.
>>
>> Is it possible to provide more than one field to these aggregates? 
>> Like in SQL, where you can provide more fields ("row" is optional):
>>
>> ARRAY_AGG(row(table.field1, table.field2))
>>
>> Best wishes,
>> Tomasz
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c7c778b5-b72e-4604-be72-14d6db2e3d18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to