Opened 5 years ago

Last modified 3 years ago

#14357 new Bug

Prevent innapropriate order-based grouping on values+annotate queries

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

Description

class Foo(models.Model):
    name = models.TextField()
    type = models.TextField()

    class Meta:
        ordering = ["name",]

Foo.objects.all().values("type").annotate(models.Count("name"))

The above code fails to generate the expected result without adding .order_by() to the query set.

Ticket #10574 called this fixed with documentation, but indicated a proper fix would be preferred once backwards compatibility wasn't an issue. Lest this fall off the radar for being closed, I am putting this ticket in for 2.0.

Attachments (1)

unordering.patch (813 bytes) - added by outofculture 5 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 5 years ago by outofculture

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

possible fallback orderings:

  • pk
  • [the ordering fields] & [the fields referenced in the .values]

comment:2 Changed 5 years ago by outofculture

(that was a set & operator. sorry for the confusing notation.)

possible fallback orderings:

  • pk
  • only those fields which are in both the ordering and the values
  • all fields in the ordering which are not also in an annotate

Changed 5 years ago by outofculture

comment:3 Changed 5 years ago by outofculture

attached unordering.patch is a poor attempt at changing the ordering.

comment:4 Changed 5 years ago by russellm

  • Has patch set
  • milestone 2.0 deleted
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:7 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:8 Changed 3 years ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top