summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Beaulé <beauleetienne0@gmail.com>2020-10-19 17:10:06 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-21 20:54:53 +0200
commit509d9da26fb92a8d566ec105ff40bb024803ceaa (patch)
tree0f3138173705775a0db082ee0df8101beda178bf
parent257f8495d6c93e30ab0f52af4c488d7344bcf112 (diff)
Fixed #26390 -- Disabled grouping by Random().
Thanks to Tzu-ping Chung for the tests.
-rw-r--r--AUTHORS1
-rw-r--r--django/db/models/functions/math.py3
-rw-r--r--tests/aggregation/tests.py15
3 files changed, 19 insertions, 0 deletions
diff --git a/AUTHORS b/AUTHORS
index f1a4315c8d..5f1ca7cceb 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -293,6 +293,7 @@ answer newbie questions, and generally made Django that much better:
Erwin Junge <erwin@junge.nl>
Esdras Beleza <linux@esdrasbeleza.com>
Espen Grindhaug <http://grindhaug.org/>
+ Étienne Beaulé <beauleetienne0@gmail.com>
Eugene Lazutkin <http://lazutkin.com/blog/>
Evan Grim <https://github.com/egrim>
Fabrice Aneche <akh@nobugware.com>
diff --git a/django/db/models/functions/math.py b/django/db/models/functions/math.py
index 304e112225..15915f4b7c 100644
--- a/django/db/models/functions/math.py
+++ b/django/db/models/functions/math.py
@@ -154,6 +154,9 @@ class Random(NumericOutputFieldMixin, Func):
def as_sqlite(self, compiler, connection, **extra_context):
return super().as_sql(compiler, connection, function='RAND', **extra_context)
+ def get_group_by_cols(self, alias=None):
+ return []
+
class Round(Transform):
function = 'ROUND'
diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
index f8aeceb2d0..a759fd4d36 100644
--- a/tests/aggregation/tests.py
+++ b/tests/aggregation/tests.py
@@ -1315,3 +1315,18 @@ class AggregateTestCase(TestCase):
# with self.assertNumQueries(1) as ctx:
# self.assertSequenceEqual(books_qs, [book])
# self.assertEqual(ctx[0]['sql'].count('SELECT'), 2)
+
+ def test_aggregation_random_ordering(self):
+ """Random() is not included in the GROUP BY when used for ordering."""
+ authors = Author.objects.annotate(contact_count=Count('book')).order_by('?')
+ self.assertQuerysetEqual(authors, [
+ ('Adrian Holovaty', 1),
+ ('Jacob Kaplan-Moss', 1),
+ ('Brad Dayley', 1),
+ ('James Bennett', 1),
+ ('Jeffrey Forcier', 1),
+ ('Paul Bissex', 1),
+ ('Wesley J. Chun', 1),
+ ('Stuart Russell', 1),
+ ('Peter Norvig', 2),
+ ], lambda a: (a.name, a.contact_count), ordered=False)