Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added sanity-checking of annotation alias names.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14092 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1fe1af93a51f10be832704ec96efc8e2ba277b3e 1 parent ed97575
Carl Meyer authored October 09, 2010
11  django/db/models/query.py
@@ -619,8 +619,19 @@ def annotate(self, *args, **kwargs):
619 619
         with data aggregated from related fields.
620 620
         """
621 621
         for arg in args:
  622
+            if arg.default_alias in kwargs:
  623
+                raise ValueError("The %s named annotation conflicts with the "
  624
+                                 "default name for another annotation."
  625
+                                 % arg.default_alias)
622 626
             kwargs[arg.default_alias] = arg
623 627
 
  628
+        names = set([f.name for f in self.model._meta.fields])
  629
+        for aggregate in kwargs:
  630
+            if aggregate in names:
  631
+                raise ValueError("The %s annotation conflicts with a field on "
  632
+                    "the model." % aggregate)
  633
+
  634
+
624 635
         obj = self._clone()
625 636
 
626 637
         obj._setup_aggregate_query(kwargs.keys())
8  tests/regressiontests/aggregation_regress/tests.py
@@ -481,6 +481,14 @@ def test_more_more(self):
481 481
             lambda b: b.name
482 482
         )
483 483
 
  484
+    def test_duplicate_alias(self):
  485
+        # Regression for #11256 - duplicating a default alias raises ValueError.
  486
+        self.assertRaises(ValueError, Book.objects.all().annotate, Avg('authors__age'), authors__age__avg=Avg('authors__age'))
  487
+
  488
+    def test_field_name_conflict(self):
  489
+        # Regression for #11256 - providing an aggregate name that conflicts with a field name on the model raises ValueError
  490
+        self.assertRaises(ValueError, Author.objects.annotate, age=Avg('friends__age'))
  491
+
484 492
     def test_pickle(self):
485 493
         # Regression for #10197 -- Queries with aggregates can be pickled.
486 494
         # First check that pickling is possible at all. No crash = success

0 notes on commit 1fe1af9

Please sign in to comment.
Something went wrong with that request. Please try again.