#31773: Django throws django.core.exceptions.FieldError: Expression contains 
mixed
types: IntegerField, AutoField.
-------------------------------------+-------------------------------------
               Reporter:  Thodoris   |          Owner:  nobody
  Sotiropoulos                       |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  master
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I have the following model.

 {{{#!python
 class Mallets(models.Model):
     id = models.AutoField(primary_key=True,blank=True, null=True)
     hindsight = models.ForeignKey(Hindsight, models.DO_NOTHING,
 blank=True, null=True)
     believer = models.IntegerField(blank=True, null=True)
     damnably = models.IntegerField(blank=True, null=True)
     issue = models.IntegerField(blank=True, null=True)
     glover = models.TextField(blank=True, null=True)  # This field type is
 a guess.

     class Meta:
         db_table = 'mallets'
 }}}

 and I perform the following query on Django 3.2

 {{{#!python
 sheer = ExpressionWrapper((F('issue') / F('id')),
 output_field=FloatField())
 lacquer = ExpressionWrapper(Avg(F('sheer'), output_field=FloatField()),
 output_field=TextField())
 q = Mallets.objects.using('default')
 ret =
 
q.annotate(sheer=sheer).values('sheer').annotate(lacquer=Sum(F('believer'))).order_by('sheer').first()
 }}}

 Django however throws the following exception

 {{{
 Traceback (most recent call last):
   File "driver_sqlite.py", line 25, in <module>
     ret2 =
 
ret1.annotate(sheer=sheer).values('sheer').annotate(lacquer=Sum('believer')).order_by('sheer').first()
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/query.py", line 678, in
 first
     for obj in (self if self.ordered else self.order_by('pk'))[:1]:
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/query.py", line 287, in
 __iter__
     self._fetch_all()
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/query.py", line 1305, in
 _fetch_all
     self._result_cache = list(self._iterable_class(self))
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/query.py", line 111, in
 __iter__
     for row in compiler.results_iter(chunked_fetch=self.chunked_fetch,
 chunk_size=self.chunk_size):
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line
 1113, in results_iter
     results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch,
 chunk_size=chunk_size)
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line
 1148, in execute_sql
     sql, params = self.as_sql()
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line 498,
 in as_sql
     extra_select, order_by, group_by = self.pre_sql_setup()
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line 60,
 in pre_sql_setup
     group_by = self.get_group_by(self.select + extra_select, order_by)
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line 142,
 in get_group_by
     sql, params = expr.select_format(self, sql, params)
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/expressions.py", line 388,
 in select_format
     if hasattr(self.output_field, 'select_format'):
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/utils/functional.py", line 48, in
 __get__
     res = instance.__dict__[self.name] = self.func(instance)
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/expressions.py", line 269,
 in output_field
     output_field = self._resolve_output_field()
   File "/home/.env/lib/python3.6/site-
 packages/Django-3.2-py3.6.egg/django/db/models/expressions.py", line 309,
 in _resolve_output_field
     source.__class__.__name__,
 django.core.exceptions.FieldError: Expression contains mixed types:
 IntegerField, AutoField. You must set output_field.
 }}}

 Note that when I run the query above on Django 3.0.8, this query runs as
 expected. So is this a regression in Django 3.2?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31773>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.392633f4bc4d2fe1905138a189ffd90c%40djangoproject.com.

Reply via email to