Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#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 Simon Charette, 4 years ago

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

This is likely a regression caused by 1e38f1191de21b6e96736f58df57dfb851a28c1f

Stefanos, could you confirm the following patch addresses your issue

  • django/db/models/expressions.py

    diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
    index a9768919a2..90d90119d0 100644
    a b class Expression(BaseExpression, Combinable):  
    421421
    422422_connector_combinators = {
    423423    connector: [
     424        (fields.IntegerField, fields.IntegerField, fields.IntegerField),
    424425        (fields.IntegerField, fields.DecimalField, fields.DecimalField),
    425426        (fields.DecimalField, fields.IntegerField, fields.DecimalField),
    426427        (fields.IntegerField, fields.FloatField, fields.FloatField),

comment:2 by Mariusz Felisiak, 4 years ago

Severity: NormalRelease blocker

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

comment:3 by Mariusz Felisiak, 4 years ago

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

comment:4 by StefanosChaliasos, 4 years ago

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

comment:5 by Matt Hegarty, 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 Matt Hegarty, 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 Simon Charette, 4 years ago

Owner: changed from nobody to Simon Charette
Status: newassigned

comment:8 by Simon Charette, 4 years ago

Has patch: set
Last edited 4 years ago by Mariusz Felisiak (previous) (diff)

comment:9 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady 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:10 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 38fce49c:

Fixed #31919 -- Resolved output_field of IntegerField subclasses combinations.

comment:11 by Matt Hegarty, 4 years ago

Cc: Matt Hegarty added

I have raised a related bug (#31967)

comment:12 by Matt Hegarty, 4 years ago

Cc: Matt Hegarty removed

comment:13 by Matt Hegarty, 4 years ago

Cc: Matt Hegarty added
Note: See TracTickets for help on using tickets.
Back to Top