﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30246	Using an annotated field calculated with django.db.models.functions.Extract in aggregate results in ProgrammingError	Jan Baryła	nobody	"Aggregating most annotated fields works as expected, but if I put a DateTimeField through Extract during the annotate step, I get a ProgrammingError when trying to aggregate.

**models.py**
{{{
class MyModel(models.Model):
    usage_time = models.DateTimeField()
    usage = models.FloatField()
}}}

I would like to take the whole queryset, and calculate hourly usages. I figured using the django.db.models.functions.Extract transform would suit my needs well. This is the sample piece of code that, in a perfect scenario, would give me a dictionary filled with key value pairs, where key is the hour, and value is the sum of usages measured in that hour. 

{{{
    hour_aggregates = {}
    for i in range(24):
        hour_aggregates['{}_{}'.format(""am"" if i < 12 else ""pm"", i)] = Sum(""usage"", filter=Q(hour=i))
    usages = MyModel.objects.annotate(hour=Extract(""usage_time"", ""hour"")).aggregate(**hour_aggregates)
}}}

Unfortunately, I get the following error:

{{{
Traceback (most recent call last):
  File ""/home/jan/project/env/lib/python3.6/site-packages/django/db/backends/utils.py"", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column ""__col2"" does not exist
LINE 1: ...CT ""package_mymodel"".""id"" AS Col1, EXTRACT('hour' FROM ""__col2"" A...
}}}

This occured to me while using Django 2.1.7. It doesn't work on 2.2b1, but I have tested this solution on Django 1.8 and it works, which is why I am filing this bug report. My Python version is 3.6.7 and I'm using PostgreSQL 10.6."	Bug	closed	Database layer (models, ORM)	3.0	Normal	fixed	query, aggregate, extract, annotate, postgresql	Simon Charette	Accepted	1	0	0	0	0	0
