#31919 closed Bug (fixed)
Django fails with FieldError: Expression contains mixed types: IntegerField, AutoField. You must set output_field.
Reported by: | StefanosChaliasos | Owned by: | Simon Charette |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Release blocker | Keywords: | |
Cc: | Matt Hegarty | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
Change History (13)
comment:1 by , 4 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 4 years ago
Severity: | Normal → Release blocker |
---|
I confirmed that it's a regression in 1e38f1191de21b6e96736f58df57dfb851a28c1f.
comment:3 by , 4 years ago
Stefanos, Would you like to prepare a patch? Simon's proposition works for me.
comment:5 by , 4 years ago
I also saw this issue in master (I use django-money):
django.core.exceptions.FieldError: Expression contains mixed types: MoneyField, IntegerField. You must set output_field.
Works fine in 3.1
comment:6 by , 4 years ago
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.
comment:7 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:9 by , 4 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
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.
comment:12 by , 4 years ago
Cc: | removed |
---|
comment:13 by , 4 years ago
Cc: | added |
---|
This is likely a regression caused by 1e38f1191de21b6e96736f58df57dfb851a28c1f
Stefanos, could you confirm the following patch addresses your issue
django/db/models/expressions.py