[Django] #31919: Django fails with FieldError: Expression contains mixed types: IntegerField, AutoField. You must set output_field.

16 views
Skip to first unread message

Django

unread,
Aug 20, 2020, 11:41:25 AM8/20/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
---------------------------------------------+------------------------
Reporter: StefanosChaliasos | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
I have the following query.


{{{
expr = Value(3) * F('id')
o = Model.objects.using('default')
res = o.values('field_name').annotate(expr=expr).values('expr')
print(res)
}}}

Unfortunately, this query crashes with a FieldError exception. The track
trace is


{{{
Traceback (most recent call last):
File "example.py", line 28, in <module>
print(res)
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 1316, 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
1115, 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
1147, 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 55,
in pre_sql_setup
self.setup_query()
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line 46,
in setup_query
self.select, self.klass_info, self.annotation_col_map =
self.get_select()
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/sql/compiler.py", line 267,
in get_select
sql, params = col.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 385,
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 266,
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 463,
in _resolve_output_field
return super()._resolve_output_field()
File "/home/.env/lib/python3.6/site-
packages/Django-3.2-py3.6.egg/django/db/models/expressions.py", line 306,
in _resolve_output_field
source.__class__.__name__,
django.core.exceptions.FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
}}}

This should be a regression bug because the previous query works in Django
3.1

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

Django

unread,
Aug 20, 2020, 2:53:13 PM8/20/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | 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 Simon Charette):

* type: Uncategorized => Bug
* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Accepted


Comment:

This is likely a regression caused by
1e38f1191de21b6e96736f58df57dfb851a28c1f

Stefanos, could you confirm the following patch addresses your issue

{{{#!diff
diff --git a/django/db/models/expressions.py
b/django/db/models/expressions.py
index a9768919a2..90d90119d0 100644
--- a/django/db/models/expressions.py
+++ b/django/db/models/expressions.py
@@ -421,6 +421,7 @@ class Expression(BaseExpression, Combinable):

_connector_combinators = {
connector: [
+ (fields.IntegerField, fields.IntegerField, fields.IntegerField),
(fields.IntegerField, fields.DecimalField, fields.DecimalField),
(fields.DecimalField, fields.IntegerField, fields.DecimalField),
(fields.IntegerField, fields.FloatField, fields.FloatField),
}}}

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

Django

unread,
Aug 21, 2020, 12:06:27 AM8/21/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: master
(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):

* severity: Normal => Release blocker


Comment:

I confirmed that it's a regression in
1e38f1191de21b6e96736f58df57dfb851a28c1f.

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

Django

unread,
Aug 21, 2020, 1:17:19 AM8/21/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: master
(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
-------------------------------------+-------------------------------------

Comment (by felixxm):

Stefanos, Would you like to prepare a patch? Simon's proposition works for
me.

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

Django

unread,
Aug 21, 2020, 4:34:04 AM8/21/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: master
(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
-------------------------------------+-------------------------------------

Comment (by StefanosChaliasos):

Yes, that patch fixed the issue. Thanks a lot!

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

Django

unread,
Aug 24, 2020, 6:06:33 AM8/24/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: nobody

Type: Bug | Status: new
Component: Database layer | Version: master
(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
-------------------------------------+-------------------------------------

Comment (by Matt Hegarty):

I can confirm that the above patch does not fix the issue I have seen.
The issue does not occur in the 3.1 release.

--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:6>

Django

unread,
Aug 30, 2020, 10:04:27 PM8/30/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: Simon
| Charette
Type: Bug | Status: assigned

Component: Database layer | Version: master
(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 Simon Charette):

* owner: nobody => Simon Charette
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:7>

Django

unread,
Aug 30, 2020, 10:29:25 PM8/30/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: master
(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 Simon Charette):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:8>

Django

unread,
Aug 31, 2020, 12:47:57 AM8/31/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: master
(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 felixxm):

* stage: Accepted => Ready for checkin


Comment:

Matt, combination of `MoneyField` and `IntegerField` works for me, because
`MoneyField` is a subclass of `DecimalField`. Please open a separate issue
if you can provide a sample project that reproduces your issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:9>

Django

unread,
Aug 31, 2020, 1:11:14 AM8/31/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: Simon
| Charette
Type: Bug | Status: closed

Component: Database layer | Version: master
(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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"38fce49c82858d2cac810679d1c0e518840a8615" 38fce49c]:
{{{
#!CommitTicketReference repository=""
revision="38fce49c82858d2cac810679d1c0e518840a8615"
Fixed #31919 -- Resolved output_field of IntegerField subclasses
combinations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:10>

Django

unread,
Aug 31, 2020, 5:33:36 AM8/31/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: master
(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 Matt Hegarty):

* cc: Matt Hegarty (added)


Comment:

I have raised a related bug (#31967)

--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:11>

Django

unread,
Aug 31, 2020, 5:34:53 AM8/31/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: master
(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 Matt Hegarty):

* cc: Matt Hegarty (removed)


--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:12>

Django

unread,
Aug 31, 2020, 5:36:27 AM8/31/20
to django-...@googlegroups.com
#31919: Django fails with FieldError: Expression contains mixed types:
IntegerField, AutoField. You must set output_field.
-------------------------------------+-------------------------------------
Reporter: StefanosChaliasos | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: master
(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 Matt Hegarty):

* cc: Matt Hegarty (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:13>

Reply all
Reply to author
Forward
0 new messages