#32032: Aggregate on union of querysets generates invalid SQL
-------------------------------------+-------------------------------------
     Reporter:  Jaap Roes            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  aggregate union      |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Jaap Roes):

 I've been able to work around this issue by manually creating aliases for
 the colums I want to aggregate:

 {{{
 def get_entity_permissions(user, entity):
     querysets = [
         EntityPermissions.objects.filter(
             entity_id=entity.id, user=user
         ).values(
             _can_discombobulate=F('can_discombobulate'),
             _can_frobnicate=F('can_frobnicate')
         ),
         OrganisationEntityPermissions.objects.filter(
             organisation_id=entity.organisation_id, user=user
         ).values(
             _can_discombobulate=F('can_discombobulate'),
             _can_frobnicate=F('can_frobnicate')
     ]

     if entity.partner_id:
         querysets.append(
             PartnerEntityPermissions.objects.filter(
                 partner_id=entity.partner_id, user=user
             ).values(
                 _can_discombobulate=F('can_discombobulate'),
                 _can_frobnicate=F('can_frobnicate')
             )
         )

     qs = functools.reduce(lambda qs1, qs2: qs1.union(qs2), querysets)

     return qs.aggregate(
         can_discombobulate=Coalesce(BoolOr('_can_discombobulate'), False),
         can_frobnicate=Coalesce(BoolOr('_can_frobnicate'), False)
     )
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32032#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.0d9ad962b47f79fdb2015fadc85e6c60%40djangoproject.com.

Reply via email to