From ccb243847e300283aca23af5eb3b6b4f2bdcfb28 Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Fri, 17 Jun 2022 09:19:49 +0200 Subject: [PATCH] [4.1.x] Refs #28897 -- Added test for QuerySet.update() on querysets ordered by inline m2m annotation. Backport of f4680a112d01d85540411673eade31f37712d0a6 from main --- django/db/backends/mysql/features.py | 5 +++++ tests/update/models.py | 1 + tests/update/tests.py | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index 53c0652fce..9583910b1e 100644 --- a/django/db/backends/mysql/features.py +++ b/django/db/backends/mysql/features.py @@ -105,6 +105,11 @@ class DatabaseFeatures(BaseDatabaseFeatures): "scalar value but it's not implemented (#25287).": { "expressions.tests.FTimeDeltaTests.test_durationfield_multiply_divide", }, + "UPDATE ... ORDER BY syntax on MySQL/MariaDB does not support ordering by" + "related fields.": { + "update.tests.AdvancedTests." + "test_update_ordered_by_inline_m2m_annotation", + }, } if "ONLY_FULL_GROUP_BY" in self.connection.sql_mode: skips.update( diff --git a/tests/update/models.py b/tests/update/models.py index 3993763eca..d7452dc302 100644 --- a/tests/update/models.py +++ b/tests/update/models.py @@ -41,6 +41,7 @@ class Foo(models.Model): class Bar(models.Model): foo = models.ForeignKey(Foo, models.CASCADE, to_field="target") m2m_foo = models.ManyToManyField(Foo, related_name="m2m_foo") + x = models.IntegerField(default=0) class UniqueNumber(models.Model): diff --git a/tests/update/tests.py b/tests/update/tests.py index 15a657fab0..0c3a399514 100644 --- a/tests/update/tests.py +++ b/tests/update/tests.py @@ -225,6 +225,13 @@ class AdvancedTests(TestCase): new_name=annotation, ).update(name=F("new_name")) + def test_update_ordered_by_inline_m2m_annotation(self): + foo = Foo.objects.create(target="test") + Bar.objects.create(foo=foo) + + Bar.objects.order_by(Abs("m2m_foo")).update(x=2) + self.assertEqual(Bar.objects.get().x, 2) + @unittest.skipUnless( connection.vendor == "mysql",