Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#10089 closed (fixed)

Aggregation breaks QuerySet.count() with empty results

Reported by: Kyle Fox Owned by: david
Component: Database layer (models, ORM) Version: master
Severity: Keywords: queryset, execute_sql, aggregation, count, filter, in
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When calling count() after performing an __in=[] filter, a TypeError is thrown (see example). This is because BaseQuery.get_aggregation is attempting to zip query.execute_sql(SINGLE) (line 301), which can evaluate to None in Line 1933:

# Works, even if the filter returns no results.
User.objects.filter(id__in=[1,2,3]).count()
User.objects.filter(id__in=[99,100,101]).count()

# Breaks: Filtering with an empty list and then calling `count()` throws an error.
User.objects.filter(id__in=[]).count()

I'm not sure how to fix this problem. I'm assuming that a return value of None is the intended behavior of execute_sql in this case, and that get_aggregation should be make more robust to handle a result of None.

Attachments (1)

patch_django_10089.20090121diff (1.4 KB) - added by david 5 years ago.
First naive approach

Download all attachments as: .zip

Change History (4)

Changed 5 years ago by david

First naive approach

comment:1 Changed 5 years ago by david

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to david
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [9786]) Fixed #10089 -- Corrected handling of aggregates when the query set contains no items (and the cursor returns None). Thanks to Kyle Fox for the report, and david for the initial patch.

comment:3 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.