#26898: annotate() with two Count('related_object') gives both as the bigger one
----------------------------------------------+----------------------------
Reporter: SonOfLilit | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Keywords: count,annotate
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+----------------------------
Minimal repro: https://github.com/sonoflilit/django-count-bug
```python
def test_bug(self):
a = A.objects.create()
B.objects.create(a=a)
B.objects.create(a=a)
C.objects.create(a=a)
# duh...
a = A.objects.annotate(b_count=Count('b'), c_count=Count('c')).get()
self.assertEquals(2, a.b_count)
# 2?! It's clearly 1!
self.assertEquals(1, a.c_count)
```
```sql
SELECT "bug_a"."id",
Count("bug_b"."id") AS "b_count",
Count("bug_c"."id") AS "c_count"
FROM "bug_a"
LEFT OUTER JOIN "bug_b"
ON ( "bug_a"."id" = "bug_b"."a_id" )
LEFT OUTER JOIN "bug_c"
ON ( "bug_a"."id" = "bug_c"."a_id" )
GROUP BY "bug_a"."id"
```
I think it should be:
```sql
SELECT "bug_a"."id",
Count(distinct "bug_b"."id") AS "b_count",
Count(distinct "bug_c"."id") AS "c_count"
FROM "bug_a"
LEFT OUTER JOIN "bug_b"
ON ( "bug_a"."id" = "bug_b"."a_id" )
LEFT OUTER JOIN "bug_c"
ON ( "bug_a"."id" = "bug_c"."a_id" )
GROUP BY "bug_a"."id"
```
Reproduces on `sqlite3` as well as heroku-hosted `postgresql`.
--
Ticket URL: <https://code.djangoproject.com/ticket/26898>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/053.d068aa531c35da1b0ac548235d6df22e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.