Annotating multiple Sum() objects gives wrong answer
|Reported by:||tBuLi||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
This is best explained with an example:
queryset = Member.objects.annotate(payed=Sum('payment__amount')) queryset = Member.objects.annotate(discount=Sum('membership__discount__amount'))
These give the right payed and discount amount for the member, whereas
queryset = Member.objects.annotate(discount=Sum('membership__discount__amount'), payed=Sum('payment__amount'))
gives a value for payed which is a factor 2 higher, and the discount is a factor 3 higher. Using exactly the same syntax with Count() gives the correct answer in both cases, so I suspect something goes wrong with Sum().
using distinct = True does not matter.