Opened 10 years ago

Last modified 10 years ago

#24570 closed Bug

`group_by` clause does not resolve keywords defined in `extra` clause. — at Version 4

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

Description (last modified by user0007)

Hello,

during migrating from Django 1.7.5 to 1.8 I found a bug(?)

    truncate_date = connection.ops.date_trunc_sql('day', 'created_at')

    qs = MyModel.objects.filter(
        user_id=1
    ).annotate(
        max_date=models.Max('created_at')
    ).extra(
        {'created_at': truncate_date}
    ).values(
        'product_id', 'created_at', 'max_date'
    )
    qs.query.group_by = ['product_id', truncate_date]
    qs.order_by('-max_date')[:10]

The result is:

FieldError: Cannot resolve keyword u"CAST(DATE_FORMAT(created_at, '%%Y-%%m-%%d 00:00:00') AS DATETIME)" into field.

This code works perfectly on Django 1.7.5

Traceback:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 138, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__
    self._fetch_all()
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 1085, in iterator
    for row in self.query.get_compiler(self.db).results_iter():
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 783, in results_iter
    results = self.execute_sql(MULTI)
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
    sql, params = self.as_sql()
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 367, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup()
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 51, in pre_sql_setup
    group_by = self.get_group_by(self.select + extra_select, order_by)
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 102, in get_group_by
    expressions.append(self.query.resolve_ref(expr))
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1522, in resolve_ref
    self.get_initial_alias(), reuse)
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1461, in setup_joins
    names, opts, allow_many, fail_on_missing=True)
  File "/home/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1386, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
FieldError: Cannot resolve keyword u"CAST(DATE_FORMAT(created_at, '%%Y-%%m-%%d 00:00:00') AS DATETIME)" into field.

Change History (4)

comment:1 by user0007, 10 years ago

Description: modified (diff)

comment:2 by user0007, 10 years ago

Description: modified (diff)

comment:3 by user0007, 10 years ago

Summary: Cannot resolve keyword "CAST" in group_by clause.`group_by` clause does not resolve keywords defined in `extra` clause.

comment:4 by user0007, 10 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top