#32645: order_by().update() support on MySQL / MariaDB fails with multi-table inheritance -------------------------------------+------------------------------------- Reporter: Matt | Owner: nobody Westcott | Type: Bug | Status: new Component: Database | Version: 3.2 layer (models, ORM) | Severity: Normal | Keywords: Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- The support for respecting queryset ordering on update queries in MySQL / MariaDB (added in #31573, 779e615e362108862f1681f965ee9e4f1d0ae6d2) does not account for multi-table inheritance setups where the columns in the ordering exist in a different table from the one being updated. This causes failures on some queries that worked prior to Django 3.2.
Testing against MySQL 8.0.23, and given the model definitions: {{{ class Place(models.Model): name = models.CharField(max_length=255) class Restaurant(Place): stars = models.IntegerField() }}} the query `Restaurant.objects.order_by('name').update(stars=3)` fails with `django.db.utils.OperationalError: (1054, "Unknown column 'core_place.name' in 'order clause'")`. (Obviously in this example the `order_by` clause is somewhat spurious, but in a real-world setup it could be introduced by a custom manager on the Place model, for example.) Meanwhile, `Restaurant.objects.order_by('name').update(name='Pizza Hut')` fails with `django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY `core_place`.`name` ASC' at line 1")`. In this case, the base SQLUpdateCompiler class returns an empty string to denote that no UPDATE is needed on the `restaurant` table (the UPDATE on `place` would happen in a subsequent call to `as_sql`), but the MySQL backend is appending the ORDER BY clause to that empty string. -- Ticket URL: <https://code.djangoproject.com/ticket/32645> 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 django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.4d002b19540cd3a025ec6b985a16b96c%40djangoproject.com.