#24171: (1054, "Unknown column '__col1' in 'field list'") when using values,
annotate and aggrregate
----------------------------------------------+-----------------------
     Reporter:  abdulhaq-e                    |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.8alpha1
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+-----------------------
 I will try my best here to explain when this comes up:

 Say I have the following test model:
 {{{
 class Test(models.Model):

     fieldA = models.ForeignKey(AnotherModel)
     fieldB = models.IntegerField()
 }}}

 I want to sum the result of multiplying `fieldB` and `fieldC` where the
 latter is obtained by spanning the relationship as such (for example):
 `fieldA__ForeignKey2__ForeignKey3__fieldC`

 To do this:

 {{{
 
Test.objects.aggregate(Sum(F(`fieldA__ForeignKey2__ForeignKey3__fieldC`)*F('fieldB')))
 }}}

 The above will work fine:

 But I want to use the new annotations features in v1.8 to make the
 relationship spanning shorter:

 {{{
 
Test.objects.annotate(fieldC=F(`fieldA__ForeignKey2__ForeignKey3__fieldC`)).aggregate(Sum(F('fieldC')*F('fieldB')))
 }}}

 This will also work fine. However, I want a `ValuesQuerySet` (i.e. I want
 some specific fields). This will NOT WORK:

 {{{
 
Test.objects.annotate(fieldC=F(`fieldA__ForeignKey2__ForeignKey3__fieldC`)).values('fieldC',
 'fieldB').aggregate(Sum(F('fieldC')*F('fieldB')))
 }}}
 This will give the error in the title: `(1054, "Unknown column '__col1' in
 'field list'")`. The error is definitely to do with fieldB and here is how
 I proved it!:

 This works:
 {{{
 
Test.objects.annotate(fieldC=F(`fieldA__ForeignKey2__ForeignKey3__fieldC`)).values('fieldC',
 'fieldB').aggregate(Sum(F('fieldC')))
 }}}

 And surprisingly, giving an alternate annotation to fieldB works! (of
 course I can't use the field name as an alias name):
 {{{
 Test.objects.annotate(fieldC=F(`fieldA__ForeignKey2__ForeignKey3__fieldC`),
 FIELDB=F('fieldB')).values('fieldC',
 'FIELDB').aggregate(Sum(F('fieldC')*F('FIELDB')))
 }}}

 Is this normal behaviour!?

--
Ticket URL: <https://code.djangoproject.com/ticket/24171>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.65772809d094fe315f92de5f524467b4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to