diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index f2e4e1f1f4d..471853b1a3c 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 f3b2b3da415..b6574eab2e9 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