Opened 7 years ago

Closed 5 years ago

#28408 closed Bug (fixed)

Incorrect error message with QuerySet.update() that requires a join on an annotated F expression

Reported by: Kevin Marsh Owned by: Can Sarıgöl
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: annotate, F, update
Cc: Can Sarıgöl Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Asif Saifuddin Auvi)

I ran into a bit of a nasty error yesterday on Django 1.11, Postgres 9.5 where I was trying to do an update using an F expression where the value in that expression referred to an annotation of an annotation, each using a Case statement inside the annotation. The error message it was giving me was something like the following:

ProgrammingError                          Traceback (most recent call last)
<ipython-input-1-29e48364245c> in <module>()
----> 1 MyModel.objects.causing_problems()

/usr/src/app/apps/myapp/managers.py in causing_problems(self)
    833                 ).update(        
--> 834                         my_field=F('my_annotated_field'),
    835                 )
    836 

/usr/src/app/apps/django/db/models/query.py in update(self, **kwargs)
    645         query._annotations = None
    646         with transaction.atomic(using=self.db, savepoint=False):
--> 647             rows = query.get_compiler(self.db).execute_sql(CURSOR)
    648         self._result_cache = None
    649         return rows

/usr/src/app/apps/django/db/models/sql/compiler.py in execute_sql(self, result_type)
   1189         related queries are not available.
   1190         """
-> 1191         cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
   1192         try:
   1193             rows = cursor.rowcount if cursor else 0

/usr/src/app/apps/django/db/models/sql/compiler.py in execute_sql(self, result_type, chunked_fetch)
    884                 # silencing when dropping Python 2 compatibility.
    885                 pass
--> 886             raise original_exception
    887 
    888         if result_type == CURSOR:

ProgrammingError: missing FROM-clause entry for table "myapp_mymodel"
LINE 1: ...false END, "a_field" = CASE WHEN CASE WHEN ("myapp_my...

Spent a while trying to debug where it was going wrong, ended up narrowing down the problem to trying to update a field using an F expression on an annotated field which included a join in it. I'm not sure if better error message would have helped in my case but in the much simpler test attached it'd be useful if the first and second assertions raised the same or similar error message.

Attachments (1)

unclear_f_annotations_error_message_failing_test.diff (1.0 KB ) - added by Kevin Marsh 7 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 by Tim Graham, 7 years ago

Component: Error reportingDatabase layer (models, ORM)
Owner: set to nobody
Summary: Inconsistant error message for trying to update an annotated F expressionIncorrect error message with QuerySet.update() that requires a join on an annotated F expression
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by Can Sarıgöl, 5 years ago

hi, I tried to fix on version 2. PR

comment:3 by Can Sarıgöl, 5 years ago

Cc: Can Sarıgöl added

comment:4 by Can Sarıgöl, 5 years ago

Has patch: set

comment:5 by Asif Saifuddin Auvi, 5 years ago

Description: modified (diff)
Needs documentation: set
Version: 1.11master

comment:6 by Can Sarıgöl, 5 years ago

Owner: changed from nobody to Can Sarıgöl
Status: newassigned

comment:7 by Asif Saifuddin Auvi, 5 years ago

Needs documentation: unset

comment:8 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:9 by Can Sarıgöl, 5 years ago

Patch needs improvement: unset

comment:10 by Mariusz Felisiak, 5 years ago

Original ticket #26539.

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In f03b7bd1:

Fixed #28408 -- Added error message when updating with annotated expressions on joined fields.

Co-Authored-By: Simon Charette <charette.s@…>

Note: See TracTickets for help on using tickets.
Back to Top