Changes between Initial Version and Version 1 of Ticket #23557, comment 7


Ignore:
Timestamp:
Oct 7, 2014, 8:45:43 AM (10 years ago)
Author:
Anssi Kääriäinen

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #23557, comment 7

    initial v1  
    22
    33In my opinion Django's group by logic should be as follows:
    4   - Group by the primary key of the table in the query by default. The group by will need to be appended by functionally dependent columns as many databases need this. This means that we can add any column from the same table to the group by, or any column from any table pointed by direct foreign key or reverse one to one field.
    5   - Group by the user's defined .values() call. Possibly allow extending it by functionally dependent columns. That is, if some table's primary key is in the values() list, then allow extension from that primary key along foreign keys and o2ofields.
    6   - If any other column is needed in the group by, then error out. This means that: `CPUJob.objects.values('project').annotate(usage=Sum('cpu_usage'), jobs=Count('id')).order_by('date')` is an error.
     4  1. Group by the primary key of the table in the query by default. The group by will need to be appended by functionally dependent columns. This means that we add any column from the same table to the group by, or any column from any table pointed by direct foreign key or reverse one to one field to the group by (there are some database specific optimizations we could use here).
     5  2. The above rule will be overridden when using .values() queries. In that case, group by the user's defined .values() call. Possibly allow extending it by functionally dependent columns. That is, if some table's primary key is in the values() list, then allow extension from that primary key along foreign keys and o2ofields similarly to 1).
     6  3. If any other column is needed in the group by, then error out. This means that: `CPUJob.objects.values('project').annotate(usage=Sum('cpu_usage'), jobs=Count('id')).order_by('date')` is an error.
    77
    88The functionally dependent columns appending means that we need to do a group by author.id, author.name on some databases even though technically group by author.id gives the same result. Similarly, we need to group by author.id, author.name, book.id, book.name when .select_related('favorite_book') is applied, where favorite_book is a foreign key from author to book.
Back to Top