From 0257426fe1fe9d146fd5813f09d909917ff59360 Mon Sep 17 00:00:00 2001 From: Nathaniel Conroy Date: Sun, 26 Nov 2023 18:36:56 -0500 Subject: [PATCH] Fixed #34992 -- Fixed DatabaseFeatures.allows_group_by_selected_pks on MariaDB with ONLY_FULL_GROUP_BY sql mode. Regression in 041551d716b69ee7c81199eee86a2d10a72e15ab. --- django/db/backends/mysql/features.py | 7 ++++++- docs/releases/4.2.8.txt | 4 ++++ tests/backends/mysql/test_features.py | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index 637bcbd17c..cafc6702eb 100644 --- a/django/db/backends/mysql/features.py +++ b/django/db/backends/mysql/features.py @@ -6,7 +6,6 @@ from django.utils.functional import cached_property class DatabaseFeatures(BaseDatabaseFeatures): empty_fetchmany_value = () - allows_group_by_selected_pks = True related_fields_match_type = True # MySQL doesn't support sliced subqueries with IN/ALL/ANY/SOME. allow_sliced_subqueries_with_in = False @@ -319,3 +318,9 @@ class DatabaseFeatures(BaseDatabaseFeatures): def has_native_uuid_field(self): is_mariadb = self.connection.mysql_is_mariadb return is_mariadb and self.connection.mysql_version >= (10, 7) + + @cached_property + def allows_group_by_selected_pks(self): + if self.connection.mysql_is_mariadb: + return "ONLY_FULL_GROUP_BY" not in self.connection.sql_mode + return True diff --git a/docs/releases/4.2.8.txt b/docs/releases/4.2.8.txt index 5b0b558c35..44d80060b5 100644 --- a/docs/releases/4.2.8.txt +++ b/docs/releases/4.2.8.txt @@ -27,3 +27,7 @@ Bugfixes * Fixed a regression in Django 4.2 where checkboxes in the admin would be centered on narrower screen widths (:ticket:`34994`). + +* Fixed a regression in Django 4.2 that caused a crash of querysets with + aggregations on MariaDB when the ``ONLY_FULL_GROUP_BY`` SQL mode was enabled + (:ticket:`34992`). diff --git a/tests/backends/mysql/test_features.py b/tests/backends/mysql/test_features.py index 96e1ed3b49..4e5c7c294f 100644 --- a/tests/backends/mysql/test_features.py +++ b/tests/backends/mysql/test_features.py @@ -28,3 +28,21 @@ class TestFeatures(TestCase): _connection.sql_mode = {"NO_AUTO_VALUE_ON_ZERO"} database_features = DatabaseFeatures(_connection) self.assertIs(database_features.allows_auto_pk_0, True) + + def test_allows_group_by_selected_pks(self): + with mock.MagicMock() as _connection: + _connection.mysql_is_mariadb = False + database_features = DatabaseFeatures(_connection) + self.assertIs(database_features.allows_group_by_selected_pks, True) + + with mock.MagicMock() as _connection: + _connection.mysql_is_mariadb = False + _connection.sql_mode = {} + database_features = DatabaseFeatures(_connection) + self.assertIs(database_features.allows_group_by_selected_pks, True) + + with mock.MagicMock() as _connection: + _connection.mysql_is_mariadb = True + _connection.sql_mode = {"ONLY_FULL_GROUP_BY"} + database_features = DatabaseFeatures(_connection) + self.assertIs(database_features.allows_group_by_selected_pks, False)