[Django] #31773: Django throws django.core.exceptions.FieldError: Expression contains mixed types: IntegerField, AutoField.

240 views
Skip to first unread message

Django

unread,
Jul 8, 2020, 4:28:49 AM7/8/20
to django-...@googlegroups.com
#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.

Django

unread,
Jul 8, 2020, 5:39:49 AM7/8/20
to django-...@googlegroups.com
#31773: ExpressionWrapper loses output_field for combined expression without an
output_field.
-------------------------------------+-------------------------------------
Reporter: Thodoris | Owner: felixxm
Sotiropoulos |
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => assigned
* severity: Normal => Release blocker
* version: master => 3.1
* owner: nobody => felixxm
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

Related to #31659.

Regression in df32fd42b84cc6dbba173201f244491b0d154a63.

--
Ticket URL: <https://code.djangoproject.com/ticket/31773#comment:1>

Django

unread,
Jul 8, 2020, 7:05:29 AM7/8/20
to django-...@googlegroups.com
#31773: ExpressionWrapper loses output_field for combined expression without an
output_field.
-------------------------------------+-------------------------------------
Reporter: Thodoris | Owner: felixxm
Sotiropoulos |
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/13165 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/31773#comment:2>

Django

unread,
Jul 9, 2020, 5:46:51 AM7/9/20
to django-...@googlegroups.com
#31773: ExpressionWrapper loses output_field for combined expression without an
output_field.
-------------------------------------+-------------------------------------
Reporter: Thodoris | Owner: felixxm
Sotiropoulos |
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31773#comment:3>

Django

unread,
Jul 9, 2020, 5:55:21 AM7/9/20
to django-...@googlegroups.com
#31773: ExpressionWrapper loses output_field for combined expression without an
output_field.
-------------------------------------+-------------------------------------
Reporter: Thodoris | Owner: felixxm
Sotiropoulos |
Type: Bug | Status: closed

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"8a6df55f2dd5131282084a4edfd48f63fbf8c69a" 8a6df55f]:
{{{
#!CommitTicketReference repository=""
revision="8a6df55f2dd5131282084a4edfd48f63fbf8c69a"
Fixed #31773 -- Fixed preserving output_field in ExpressionWrapper for
combined expressions.

Thanks Thodoris Sotiropoulos for the report and Simon Charette for the
implementation idea.

Regression in df32fd42b84cc6dbba173201f244491b0d154a63.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31773#comment:4>

Django

unread,
Jul 9, 2020, 5:56:25 AM7/9/20
to django-...@googlegroups.com
#31773: ExpressionWrapper loses output_field for combined expression without an
output_field.
-------------------------------------+-------------------------------------
Reporter: Thodoris | Owner: felixxm
Sotiropoulos |
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"e6285cac83b2aa255c9611a6536a92d10d890842" e6285cac]:
{{{
#!CommitTicketReference repository=""
revision="e6285cac83b2aa255c9611a6536a92d10d890842"
[3.1.x] Fixed #31773 -- Fixed preserving output_field in ExpressionWrapper
for combined expressions.

Thanks Thodoris Sotiropoulos for the report and Simon Charette for the
implementation idea.

Regression in df32fd42b84cc6dbba173201f244491b0d154a63.
Backport of 8a6df55f2dd5131282084a4edfd48f63fbf8c69a from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31773#comment:5>

Reply all
Reply to author
Forward
0 new messages