#31679: Django sublty produces incorrect query when the same keyword appears in
both aggregate() and annotate()
---------------------------------------------+------------------------
               Reporter:  StefanosChaliasos  |          Owner:  nobody
                   Type:  Uncategorized      |         Status:  new
              Component:  Uncategorized      |        Version:  3.0
               Severity:  Normal             |       Keywords:
           Triage Stage:  Unreviewed         |      Has patch:  0
    Needs documentation:  0                  |    Needs tests:  0
Patch needs improvement:  0                  |  Easy pickings:  0
                  UI/UX:  0                  |
---------------------------------------------+------------------------
 I have the following query:

 {{{#!python
   Model.objects.annotate(foo=F('column')).aggregate(foo=Sum(F('foo')))
   }}}

 Initially, I was running this query on SQLite and Django was producing a
 result (i.e. 0).
 When I switched to MySQL this query crushed with the following exception:

 {{{
   Traceback (most recent call last):
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/backends/utils.py", line 86, in _execute
     return self.cursor.execute(sql, params)
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/backends/mysql/base.py", line 74, in execute
     return self.cursor.execute(query, args)
   File "/dir/.env/lib/python3.8/site-packages/MySQLdb/cursors.py", line
 209, in execute
     res = self._query(query)
   File "/dir/.env/lib/python3.8/site-packages/MySQLdb/cursors.py", line
 315, in _query
     db.query(q)
   File "/dir/.env/lib/python3.8/site-packages/MySQLdb/connections.py",
 line 239, in query
     _mysql.connection.query(self, query)
 MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'foo' in
 'field list'")

 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "mysql.py", line 15, in <module>
     ret2 =
 
Model.objects.using('mysql').annotate(foo=F('column')).aggregate(foo=Sum(F('foo')))
   File "/dir/.env/lib/python3.8/site-packages/django/db/models/query.py",
 line 384, in aggregate
     return query.get_aggregation(self.db, kwargs)
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/models/sql/query.py", line 502, in get_aggregation
     result = compiler.execute_sql(SINGLE)
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/models/sql/compiler.py", line 1151, in execute_sql
     cursor.execute(sql, params)
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/backends/utils.py", line 100, in execute
     return super().execute(sql, params)
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/backends/utils.py", line 68, in execute
     return self._execute_with_wrappers(sql, params, many=False,
 executor=self._execute)
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/backends/utils.py", line 86, in _execute
     return self.cursor.execute(sql, params)
   File "/dir/.env/lib/python3.8/site-packages/django/db/utils.py", line
 90, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/backends/utils.py", line 86, in _execute
     return self.cursor.execute(sql, params)
   File "/dir/.env/lib/python3.8/site-
 packages/django/db/backends/mysql/base.py", line 74, in execute
     return self.cursor.execute(query, args)
   File "/dir/.env/lib/python3.8/site-packages/MySQLdb/cursors.py", line
 209, in execute
     res = self._query(query)
   File "/dir/.env/lib/python3.8/site-packages/MySQLdb/cursors.py", line
 315, in _query
     db.query(q)
   File "/dir/.env/lib/python3.8/site-packages/MySQLdb/connections.py",
 line 239, in query
     _mysql.connection.query(self, query)
 django.db.utils.OperationalError: (1054, "Unknown column 'foo' in 'field
 list'")
   }}}

 After examining the SQL query produced by Django, I realized that the
 query is not correct.
 Specifically, Django produced the following SQL query:

 {{{#!sql
   SELECT SUM(`foo`) AS `foo` FROM `model`
   }}}

 Instead of

 {{{#!sql
   SELECT SUM(`foo`) FROM (SELECT `model`.`column` AS `foo` FROM `model`)
 subquery
   }}}

 The issue appears when a keyword in aggregate function is the same with
 one of the keywords in annotate function. I initially thought that Django
 does not have any restriction on the keywords used in aggregate (i.e. a
 keyword in aggregate can be the same with the name of a column from the
 inspected model).

 If you think that aggregate should not conflict with the annotate, then
 Django should produce a warning as running this query in SQLite subtly
 produces an incorrect result.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31679>
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/060.fddafaf55bfaf12ff9c6a79f26848839%40djangoproject.com.

Reply via email to