Opened 16 years ago
Closed 9 years ago
#11096 closed Bug (worksforme)
.values including related fields does not work when specified after an aggregate's .annotate
Reported by: | David Gouldin | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | dgouldin@…, cmutel@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using the following models:
class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): name = models.CharField(max_length=100) author = models.ForeignKey(Author, related_name="books")
This query results in an exception:
Book.objects.annotate(num_authors=Count('author')).filter(num_authors__gt=1).values('author__name')
It looks like the GROUP BY clause of the resulting sql is not getting set correctly. This only seems to happen when .values comes after .annotate.
Change History (8)
comment:1 by , 16 years ago
Cc: | added |
---|
comment:2 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 15 years ago
Cc: | added |
---|---|
Version: | 1.0 → SVN |
comment:4 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:7 by , 12 years ago
Component: | ORM aggregation → Database layer (models, ORM) |
---|
comment:8 by , 9 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
Cannot reproduce (tried back to the current stable/1.4.x branch). I assume it's been fixed since the report.
Note:
See TracTickets
for help on using tickets.
This doesn't appear to affect sqlite, but is still a problem for postgresql using trunk. However, the problem rests with Django's ORM, because if annotate comes after values then the SQL is correct. I assume that postgres is simply stricter in what it accepts. A simple test query, using the models in the ticket:
produces the following SQL:
And raises
However, putting the annotate clause at the end results in the correct ""GROUP BY"" clause:
I think this problem is related to tickets #10060 and #12608.