#32006: QuerySet with values with annotate generates wrong SQL query
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
Konstantin Popov |
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) | Keywords: QuerySet values
Severity: Normal | annotate GROUP BY
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I use values and annotate on QuerySet to make query for my report.
When I add 'id' or 'pk' field to another fields in values it becomes the
only field in GROUP BY clause.
This, obviously, generates an invalid SQL query.
{{{
str(Currency.objects.filter(status=3).values('sifr','guid').annotate(cnt=Count('*')).query)
}}}
'SELECT "rk7data_currency"."sifr", "rk7data_currency"."guid", COUNT(*) AS
"cnt" FROM "rk7data_currency" WHERE "rk7data_currency"."status" = 3 GROUP
BY "rk7data_currency"."sifr", "rk7data_currency"."guid"'
{{{
str(Currency.objects.filter(status=3).values('sifr','guid','id').annotate(cnt=Count('*')).query)
}}}
'SELECT "rk7data_currency"."sifr", "rk7data_currency"."guid",
"rk7data_currency"."id", COUNT(*) AS "cnt" FROM "rk7data_currency" WHERE
"rk7data_currency"."status" = 3 GROUP BY "rk7data_currency"."id"'
I have checked this on several models with the same result.
I have discovered this on django 2.2.10. Upgrade to 2.2.16 and even to 3.1
does not solve the problem.
--
Ticket URL: <https://code.djangoproject.com/ticket/32006>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.94eafd905b31848e57ab667db97efc95%40djangoproject.com.