Index: tests/regressiontests/defer_regress/models.py
===================================================================
--- tests/regressiontests/defer_regress/models.py	(revision 12454)
+++ tests/regressiontests/defer_regress/models.py	(working copy)
@@ -35,6 +35,20 @@
     num = models.FloatField()
     name = models.CharField(max_length=16)
 
+# Models for 11890 -- do not change since bug includes a really
+# esoteric edge case
+class Person(models.Model):
+    first_name = models.CharField(max_length=200)
+    last_name = models.CharField(max_length=50)
+    best_friend = models.ForeignKey('self', blank=True, null=True, editable=False)
+        
+    nick_name = models.CharField(max_length=50, blank=True, null=True)
+
+class Candy(models.Model):
+    owner = models.ForeignKey(Person, related_name="candies")
+    qty = models.PositiveIntegerField(default=0)
+
+
 __test__ = {"regression_tests": """
 Deferred fields should really be deferred and not accidentally use the field's
 default value just because they aren't passed to __init__.
@@ -139,11 +153,47 @@
 # Regression for #11936 - loading.get_models should not return deferred models by default.
 >>> from django.db.models.loading import get_models
 >>> sorted(get_models(models.get_app('defer_regress')), key=lambda obj: obj._meta.object_name)
-[<class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.Leaf'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.ResolveThis'>]
+[<class 'regressiontests.defer_regress.models.Candy'>, <class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.Leaf'>, <class 'regressiontests.defer_regress.models.Person'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.ResolveThis'>]
 
 >>> sorted(get_models(models.get_app('defer_regress'), include_deferred=True), key=lambda obj: obj._meta.object_name)
-[<class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name_other_value_text'>, <class 'regressiontests.defer_regress.models.Item_Deferred_other_value_text_value'>, <class 'regressiontests.defer_regress.models.Item_Deferred_text_value'>, <class 'regressiontests.defer_regress.models.Leaf'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_name_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_second_child_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_value'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.RelatedItem_Deferred_item_id'>, <class 'regressiontests.defer_regress.models.ResolveThis'>, <class 'regressiontests.defer_regress.models.ResolveThis_Deferred_num'>]
+[<class 'regressiontests.defer_regress.models.Candy'>, <class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name_other_value_text'>, <class 'regressiontests.defer_regress.models.Item_Deferred_other_value_text_value'>, <class 'regressiontests.defer_regress.models.Item_Deferred_text_value'>, <class 'regressiontests.defer_regress.models.Leaf'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_name_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_second_child_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_value'>, <class 'regressiontests.defer_regress.models.Person'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.RelatedItem_Deferred_item_id'>, <class 'regressiontests.defer_regress.models.ResolveThis'>, <class 'regressiontests.defer_regress.models.ResolveThis_Deferred_num'>]
 
+# Regression for #11890 - Defer/only + annotate is broken
+>>> from django.db.models import Count, Avg
+>>> Item.objects.all().delete()
+>>> RelatedItem.objects.all().delete()
+>>> i = Item.objects.create(name='Bug #11890', value=1)
+>>> RelatedItem.objects.create(item=i)
+<RelatedItem: RelatedItem object>
+>>> RelatedItem.objects.create(item=i)
+<RelatedItem: RelatedItem object>
+>>> counted = Item.objects.only('name').annotate(Count('relateditem'))
+>>> counted.count()
+1
+>>> counted[0].relateditem__count
+2
+
+# Adding up some spice since result row index offset is affected by extra selections
+>>> counted = Item.objects.extra(select={'past': 'now()'}).annotate(rel_count=Count('relateditem'), avg_id=Avg('id')).extra(select={'sometime': 'now()'})
+>>> counted.count()
+1
+>>> counted[0].rel_count
+2
+
+# Rather interestingly, all those select_related and defer are required to 
+# trigger some edge case behaviour
+>>> from django.db.models import Sum;
+>>> Person.objects.all().delete()
+>>> Candy.objects.all().delete()
+>>> p = Person.objects.create(first_name='Mr', last_name='Egotistic')
+>>> p.best_friend = p
+>>> p.id = 1            # For determinism
+>>> p.save()
+>>> creations = [Candy(pk=n, owner_id=p.pk, qty=42).save() for n in xrange(10)]
+>>> Candy.objects.aggregate(Sum('qty'))['qty__sum']
+378
+>>> Person.objects.annotate(Sum('candies__qty')).select_related('best_friend').defer('first_name','best_friend__nick_name').get(pk=p.pk).candies__qty__sum
+378
 """
 }
 
