[Django] #36773: ManyToMany related manager .count() returns 0 while .all().count() returns correct value (Django 5.2.9 and 6.0.0)

4 views
Skip to first unread message

Django

unread,
Dec 4, 2025, 6:40:37 AM (yesterday) Dec 4
to django-...@googlegroups.com
#36773: ManyToMany related manager .count() returns 0 while .all().count() returns
correct value (Django 5.2.9 and 6.0.0)
-------------------------------------+-------------------------------------
Reporter: DanielDavtyan | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 6.0 | Severity: Release
| blocker
Keywords: count, django, orm | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Environment

Django: 5.2.9

Python: 3.12.x

Database: PostgreSQL

OS: (e.g. Ubuntu 22.04 – fill in)

The same code previously worked correctly on Django 4.x. After upgrading
to Django 5.x (now 6.0), I see a discrepancy between ManyToMany related
manager .count() and .all().count() in tests.

This looks similar to the regression tracked as #36197, which was fixed
for ManyToManyField related managers using a through model with to_field,
but I am still seeing the same symptom on Django 5.2.9.
--
Ticket URL: <https://code.djangoproject.com/ticket/36773>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 4, 2025, 6:44:18 AM (yesterday) Dec 4
to django-...@googlegroups.com
#36773: ManyToMany related manager .count() returns 0 while .all().count() returns
correct value (Django 5.2.9 and 6.0.0)
-------------------------------------+-------------------------------------
Reporter: Daniel Davtyan | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: count, django, orm | 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 Daniel Davtyan:

Old description:

> Environment
>
> Django: 5.2.9
>
> Python: 3.12.x
>
> Database: PostgreSQL
>
> OS: (e.g. Ubuntu 22.04 – fill in)
>
> The same code previously worked correctly on Django 4.x. After upgrading
> to Django 5.x (now 6.0), I see a discrepancy between ManyToMany related
> manager .count() and .all().count() in tests.
>
> This looks similar to the regression tracked as #36197, which was fixed
> for ManyToManyField related managers using a through model with to_field,
> but I am still seeing the same symptom on Django 5.2.9.

New description:

Environment

Django: 5.2.9

Python: 3.12.x

Database: PostgreSQL

OS: Ubuntu 22.04

The same code previously worked correctly on Django 4.x. After upgrading
to Django 5.x (now 6.0), I see a discrepancy between ManyToMany related
manager .count() and .all().count() in tests.

This looks similar to the regression tracked as #36197, which was fixed
for ManyToManyField related managers using a through model with to_field,
but I am still seeing the same symptom on Django 5.2.9.

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

Django

unread,
Dec 4, 2025, 10:10:17 AM (yesterday) Dec 4
to django-...@googlegroups.com
#36773: ManyToMany related manager .count() returns 0 while .all().count() returns
correct value (Django 5.2.9 and 6.0.0)
-------------------------------------+-------------------------------------
Reporter: Daniel Davtyan | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution: needsinfo
Keywords: count, django, orm | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

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

Comment:

Hello Daniel,

This could be a legitimate regression but without a proper set of models
instructions to reproduce it's impossible for us to assess.

As you might have notice both c3a23aa02faa1cf1d32e43d66858e793cd9ecac4 and
66e47ac69a7e71cf32eee312d05668d8f1ba24bb include tests covering scenarios
where the code behaves as expected so you might have run into particular
scenario that isn't covered yet, or you might be using a third-party app
that misbehaves, or something else might be at play here.

In other words, please help us help you, there is not much we can do with
the little details you've provided so far.
--
Ticket URL: <https://code.djangoproject.com/ticket/36773#comment:2>
Reply all
Reply to author
Forward
0 new messages