summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2015-05-05 14:44:33 +0300
committerTim Graham <timograham@gmail.com>2015-05-11 11:42:27 -0400
commitadc57632bc26cc8fe42bdb6aff463f883214980a (patch)
tree6dcf3ba58091af38a12197cf845f551728b95fe1 /tests
parentbe9d645346a20a6c394bf70d47b1b1d5c81ff530 (diff)
Fixed #24748 -- Fixed incorrect GROUP BY on MySQL in some queries
When the query's model had a self-referential foreign key, the compiler.get_group_by() code incorrectly used the self-referential foreign key's column (for example parent_id) as GROUP BY clause when it should have used the model's primary key column (id).
Diffstat (limited to 'tests')
-rw-r--r--tests/aggregation_regress/models.py5
-rw-r--r--tests/aggregation_regress/tests.py14
2 files changed, 18 insertions, 1 deletions
diff --git a/tests/aggregation_regress/models.py b/tests/aggregation_regress/models.py
index 2cad0a5486..4d675952c2 100644
--- a/tests/aggregation_regress/models.py
+++ b/tests/aggregation_regress/models.py
@@ -104,3 +104,8 @@ class Bravo(models.Model):
class Charlie(models.Model):
alfa = models.ForeignKey(Alfa, null=True)
bravo = models.ForeignKey(Bravo, null=True)
+
+
+class SelfRefFK(models.Model):
+ name = models.CharField(max_length=50)
+ parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py
index 596f69c2dc..cb469363a0 100644
--- a/tests/aggregation_regress/tests.py
+++ b/tests/aggregation_regress/tests.py
@@ -17,7 +17,7 @@ from django.utils import six
from .models import (
Alfa, Author, Book, Bravo, Charlie, Clues, Entries, HardbackBook, ItemTag,
- Publisher, Store, WithManualPK,
+ Publisher, SelfRefFK, Store, WithManualPK,
)
@@ -1277,3 +1277,15 @@ class JoinPromotionTests(TestCase):
def test_non_nullable_fk_not_promoted(self):
qs = Book.objects.annotate(Count('contact__name'))
self.assertIn(' INNER JOIN ', str(qs.query))
+
+
+class SelfReferentialFKTests(TestCase):
+ def test_ticket_24748(self):
+ t1 = SelfRefFK.objects.create(name='t1')
+ SelfRefFK.objects.create(name='t2', parent=t1)
+ SelfRefFK.objects.create(name='t3', parent=t1)
+ self.assertQuerysetEqual(
+ SelfRefFK.objects.annotate(num_children=Count('children')).order_by('name'),
+ [('t1', 2), ('t2', 0), ('t3', 0)],
+ lambda x: (x.name, x.num_children)
+ )