Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#27990 closed Bug (invalid)

QuerySet.count() raises ValueError when QuerySet.union() is used

Reported by: kapil garg Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When we get objects from a model and do union then the resulted QuerySet.count() raises ValueError due to the presence of Non-Numeric first Field.


class SomeModel(models.Model):
    name = models.CharField(max_length=10, default='example')

>>> from bug.models import SomeModel
>>> e1 = SomeModel(name='current')
>>> e2 = SomeModel()
>>> qs1 = SomeModel.objects.filter(name__startswith='ex').only('name')
>>> qs2 = SomeModel.objects.filter(name__startswith='cu').only('name')
>>> qs = qs1.union(qs2)
>>> print(qs.count())
>>> #raises ValueError : invalid literal for int() with base 10

If the first field contained in queryset is numeric then this error is not raised.

Eg. remove .only() method above on both querysets and then .count() with return correct result.

Change History (2)

comment:1 by kapil garg, 7 years ago

Resolution: invalid
Status: newclosed

I found the reason. union() does not support count().

comment:2 by Tim Graham, 7 years ago

I opened #27995 to raise a descriptive error on unsupported operations following QuerySet.union().

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