[Django] #32963: Bug: combination of filter on the annotation and random order.

17 views
Skip to first unread message

Django

unread,
Jul 26, 2021, 6:36:20 AM7/26/21
to django-...@googlegroups.com
#32963: Bug: combination of filter on the annotation and random order.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
likecodeingloveproblems |
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 |
-------------------------------------+-------------------------------------
I have a query like:
In [6]: Shop.objects\
...: .annotate(product_count = Count('ShopProduct'))\
...: .filter(product_count__gt=1)\
...: .order_by('?')
Out[6]: <QuerySet []>

but the out put is empty!
if i remove order_by('?') it works, and the same for the
filter(product_count__gt=1).
I get the SQL from connection.queries:

SELECT ..., COUNT("nakhll_market_product"."ID") AS "product_count" FROM
"nakhll_market_shop" LEFT OUTER JOIN "nakhll_market_product" ON
("nakhll_market_shop"."ID" = "nakhll_market_product"."FK_Shop_id") GROUP
BY "nakhll_market_shop"."ID", RANDOM() HAVING
COUNT("nakhll_market_product"."ID") > 1 ORDER BY RANDOM() ASC

but this SQL works correctly:

SELECT
... ,
COUNT("nakhll_market_product"."ID") AS "product_count"
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()

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

Django

unread,
Jul 26, 2021, 6:39:24 AM7/26/21
to django-...@googlegroups.com
#32963: Bug: combination of filter on the annotation and random order.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
likecodeingloveproblems |
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:

Keywords: Random + Group By | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by likecodeingloveproblems:

Old description:

New description:

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32963#comment:1>

Django

unread,
Jul 26, 2021, 6:39:52 AM7/26/21
to django-...@googlegroups.com
#32963: Bug: combination of filter on the annotation and random order.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
likecodeingloveproblems |
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Random + Group By | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by likecodeingloveproblems:

Old description:

> I have a query like:

New description:

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32963#comment:2>

Django

unread,
Jul 26, 2021, 6:43:04 AM7/26/21
to django-...@googlegroups.com
#32963: Bug: combination of filter on the annotation and random order.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
likecodeingloveproblems |
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Random + Group By | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by likecodeingloveproblems):

The difference is here:

ORM:
GROUP BY "nakhll_market_shop"."ID" **, RANDOM()** HAVING


COUNT("nakhll_market_product"."ID") > 1 ORDER BY RANDOM()

CORRECT:


GROUP BY "nakhll_market_shop"."ID" HAVING
COUNT("nakhll_market_product"."ID") > 1 ORDER BY RANDOM()

--
Ticket URL: <https://code.djangoproject.com/ticket/32963#comment:3>

Django

unread,
Jul 26, 2021, 6:43:17 AM7/26/21
to django-...@googlegroups.com
#32963: Combination of filter on the annotation and random order.

-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
likecodeingloveproblems |
Type: Bug | Status: closed

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: Random + Group By | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #26390 (fixed in Django 3.2).

--
Ticket URL: <https://code.djangoproject.com/ticket/32963#comment:4>

Reply all
Reply to author
Forward
0 new messages