Opened 5 years ago

Closed 5 years ago

#28281 closed Bug (duplicate)

Queryset.count() with a union takes the first id of the results objects

Reported by: Amir Tugendhaft Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords: Queryset, count, union
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a query I built with 2 querysets with a union between them, and I get a wrong count for them.
For each one of them the queryset.count() is correct, but after the union the queryset.count() is the first id of the objects results.
I used queeryset.query to see the raw query and its fine.

I'm attaching 2 photos so you could see the problem:

The first - showing 33 results is when I used '|' with the 2 querysets.
This workaround is not good for me, since the UNION's advantage is that I get the first query's results on top of the rest

The second - showing 25103 results is when I used the .union()

Attachments (2)

2017-06-07 (4)_LI.jpg (53.4 KB) - added by Amir Tugendhaft 5 years ago.
Using .union()
2017-06-07 (5)_LI.jpg (91.7 KB) - added by Amir Tugendhaft 5 years ago.
Using queryset1 | queryset2

Download all attachments as: .zip

Change History (3)

Changed 5 years ago by Amir Tugendhaft

Attachment: 2017-06-07 (4)_LI.jpg added

Using .union()

Changed 5 years ago by Amir Tugendhaft

Attachment: 2017-06-07 (5)_LI.jpg added

Using queryset1 | queryset2

comment:1 Changed 5 years ago by Tim Graham

Resolution: duplicate
Status: newclosed

QuerySet.count() isn't supported for unioned querysets, however, this doesn't raise an error yet, see #27995 for adding that.

Note: See TracTickets for help on using tickets.
Back to Top