{{{
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.
* 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>
* severity: Normal => Release blocker
Comment:
I confirmed that it's a regression in
1e38f1191de21b6e96736f58df57dfb851a28c1f.
--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:2>
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>
Comment (by StefanosChaliasos):
Yes, that patch fixed the issue. Thanks a lot!
--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:4>
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>
* owner: nobody => Simon Charette
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:7>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:8>
* 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>
* 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>
* cc: Matt Hegarty (added)
Comment:
I have raised a related bug (#31967)
--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:11>
* cc: Matt Hegarty (removed)
--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:12>
* cc: Matt Hegarty (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/31919#comment:13>