From 041551d716b69ee7c81199eee86a2d10a72e15ab Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 5 Nov 2022 12:41:56 -0400 Subject: [PATCH] Fixed #31331 -- Switched MySQL to group by selected primary keys. MySQL 5.7.15 supports group by functional dependences so there is no need to special case group by main table primary key anymore and special case the ONLY_FULL_GROUP_BY sql mode. --- django/db/backends/mysql/features.py | 14 +------------- django/db/models/sql/compiler.py | 4 +++- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index f2e4e1f1f4..471853b1a3 100644 --- a/django/db/backends/mysql/features.py +++ b/django/db/backends/mysql/features.py @@ -6,7 +6,7 @@ from django.utils.functional import cached_property class DatabaseFeatures(BaseDatabaseFeatures): empty_fetchmany_value = () - allows_group_by_pk = True + 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 @@ -109,18 +109,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation", }, } - if "ONLY_FULL_GROUP_BY" in self.connection.sql_mode: - skips.update( - { - "GROUP BY optimization does not work properly when " - "ONLY_FULL_GROUP_BY mode is enabled on MySQL, see #31331.": { - "aggregation.tests.AggregateTestCase." - "test_aggregation_subquery_annotation_multivalued", - "annotations.tests.NonAggregateAnnotationTestCase." - "test_annotation_aggregate_with_m2o", - }, - } - ) if self.connection.mysql_is_mariadb and ( 10, 4, diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index f3b2b3da41..b6574eab2e 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -235,7 +235,9 @@ class SQLCompiler: expressions = [ expr for expr in expressions - if expr in pks or getattr(expr, "alias", None) not in aliases + if expr in pks + or expr in having + or getattr(expr, "alias", None) not in aliases ] return expressions