Opened 7 months ago

Closed 7 months ago

Last modified 7 months ago

#35369 closed Bug (duplicate)

MySQL .union().aggregate() raises Unknown Column

Reported by: DS/Charlie Owned by: nobody
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords: union, aggregate, mysql
Cc: DS/Charlie 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 DS/Charlie)

class BugModel(models.Model):
    foo = models.CharField(max_length=255)

class BugTestCase(TestCase):
    def test_bug(self):
        a = BugModel.objects.all()
        b = BugModel.objects.all()

        ab = a.union(b)   # does NOT happen with `a | b`
        """
            (
                SELECT `bugapp_bugmodel`.`id` AS `col1`,  `bugapp_bugmodel`.`foo` AS `col2`
                FROM `bugapp_bugmodel`
            ) UNION (
                SELECT `bugapp_bugmodel`.`id` AS `col1`, `bugapp_bugmodel`.`foo` AS `col2`
                FROM `bugapp_bugmodel`
            )
        """

        q = ab.aggregate(max=Max("foo"))
        """
        SELECT MAX(`__col1`)
        FROM (
            (
                SELECT  `bugapp_bugmodel`.`id` AS `col1`, `bugapp_bugmodel`.`foo` AS `col2`
                FROM `bugapp_bugmodel`
            ) UNION (
                SELECT `bugapp_bugmodel`.`id` AS `col1`, `bugapp_bugmodel`.`foo` AS `col2`
                FROM `bugapp_bugmodel`
            )
        ) subquery

        
        Traceback (most recent call last):
          File "/private/tmp/djangobug/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 89, in _execute
            return self.cursor.execute(sql, params)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "/private/tmp/djangobug/venv/lib/python3.12/site-packages/django/db/backends/mysql/base.py", line 75, in execute
            return self.cursor.execute(query, args)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "/private/tmp/djangobug/venv/lib/python3.12/site-packages/MySQLdb/cursors.py", line 179, in execute
            res = self._query(mogrified_query)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          File "/private/tmp/djangobug/venv/lib/python3.12/site-packages/MySQLdb/cursors.py", line 330, in _query
            db.query(q)
          File "/private/tmp/djangobug/venv/lib/python3.12/site-packages/MySQLdb/connections.py", line 261, in query
            _mysql.connection.query(self, query)
        MySQLdb.OperationalError: (1054, "Unknown column '__col1' in 'field list'")
        """

        self.assertIsNone(q['max'])

Django 4.2.11 and 5.0.4
mysqlclient 2.2.4

all in a clean virtualenv on python 3.12.2

Change History (4)

comment:1 by DS/Charlie, 7 months ago

Description: modified (diff)

comment:2 by DS/Charlie, 7 months ago

Description: modified (diff)

comment:3 by DS/Charlie, 7 months ago

Description: modified (diff)

comment:4 by Simon Charette, 7 months ago

Resolution: duplicate
Status: newclosed

Per the documentation

In addition, only LIMIT, OFFSET, COUNT(*), ORDER BY, and specifying columns (i.e. slicing, count(), exists(), order_by(), and values()/values_list()) are allowed on the resulting QuerySet. Further, databases place restrictions on what operations are allowed in the combined queries.

Closing as duplicate of #28519.

Version 0, edited 7 months ago by Simon Charette (next)
Note: See TracTickets for help on using tickets.
Back to Top