#33568: Random order doesn't work correctly with aggregation
-------------------------------------+-------------------------------------
               Reporter:  Mohammad   |          Owner:  nobody
  Ali Mehdizadeh                     |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  Random + Group By
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 This query:
     Shop.objects\
     .filter(Publish = True, Available = True)\
     .annotate(product_count = Count('ShopProduct'))\
     .filter(product_count__gt=1)\
     .order_by('?').values('ID')[:12]

 and ORM generate this SQL:
 SELECT "nakhll_market_shop"."ID" FROM "nakhll_market_shop" LEFT OUTER JOIN
 "nakhll_market_product" ON ("nakhll_market_shop"."ID" =
 "nakhll_market_product"."FK_Shop_id") WHERE
 ("nakhll_market_shop"."Available" AND "nakhll_market_shop"."Publish")
 GROUP BY "nakhll_market_shop"."ID", RANDOM() HAVING
 COUNT("nakhll_market_product"."ID") > 1 ORDER BY RANDOM() ASC LIMIT 12

 but it does not return any shop,
 the correct SQL query is:
 SELECT "nakhll_market_shop"."ID" FROM "nakhll_market_shop" LEFT OUTER JOIN
 "nakhll_market_product" ON ("nakhll_market_shop"."ID" =
 "nakhll_market_product"."FK_Shop_id") WHERE
 ("nakhll_market_shop"."Available" AND "nakhll_market_shop"."Publish")
 GROUP BY "nakhll_market_shop"."ID" HAVING
 COUNT("nakhll_market_product"."ID") > 1 ORDER BY RANDOM() ASC LIMIT 12

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33568>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107017f6a114aac-e1f438d3-8d4c-4130-8813-e66b33663d3e-000000%40eu-central-1.amazonses.com.

Reply via email to