#25171: Can't update queryset after Count annotation
-------------------------------------+-------------------------------------
Reporter: fraserharris | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.6
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, update, | Triage Stage:
annotate, count | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by fraserharris):
* keywords: queryset, update, filter => queryset, update, annotate, count
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> Query:
> {{{
> >>> Category.objects.annotate(Count('tiles')).update(name='foo')
> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> File "/opt/secondfunnel/venv/local/lib/python2.7/site-
> packages/django/db/models/query.py", line 490, in update
> rows = query.get_compiler(self.db).execute_sql(None)
> File "/opt/secondfunnel/venv/local/lib/python2.7/site-
> packages/django/db/models/sql/compiler.py", line 976, in execute_sql
> cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
> File "/opt/secondfunnel/venv/local/lib/python2.7/site-
> packages/django/db/models/sql/compiler.py", line 782, in execute_sql
> cursor.execute(sql, params)
> File "/opt/secondfunnel/venv/local/lib/python2.7/site-
> packages/django/db/backends/util.py", line 69, in execute
> return super(CursorDebugWrapper, self).execute(sql, params)
> File "/opt/secondfunnel/venv/local/lib/python2.7/site-
> packages/django/db/backends/util.py", line 53, in execute
> return self.cursor.execute(sql, params)
> File "/opt/secondfunnel/venv/local/lib/python2.7/site-
> packages/django/db/utils.py", line 99, in __exit__
> six.reraise(dj_exc_type, dj_exc_value, traceback)
> File "/opt/secondfunnel/venv/local/lib/python2.7/site-
> packages/django/db/backends/util.py", line 53, in execute
> return self.cursor.execute(sql, params)
> ProgrammingError: subquery has too many columns
> LINE 1: ..." SET "name" = 'foo' WHERE "assets_category"."id" IN
> (SELECT...
> ^
> }}}
>
> Models:
> {{{
> class Category(BaseModel):
> tiles = models.ManyToManyField(Tile, related_name='categories')
> name = models.CharField(max_length=255)
>
> class Tile(Model):
> pass
> }}}
>
> SQL Query from django.db.connections.queries:
> {{{
> UPDATE "assets_category" SET "name" = \'foo\' WHERE
> "assets_category"."id" IN (SELECT U0."id", COUNT(U2."tile_id") AS
> "tiles__count" FROM "assets_category" U0 LEFT OUTER JOIN
> "assets_category_tiles" U2 ON ( U0."id" = U2."category_id" ) GROUP BY
> U0."id", U0."name")
> }}}
New description:
A Queryset that has been annotated with a Count of a ManyToMany field can
not be update'd.
Example:
{{{
>>> Category.objects.annotate(Count('tiles')).update(name='foo')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/secondfunnel/venv/local/lib/python2.7/site-
packages/django/db/models/query.py", line 490, in update
rows = query.get_compiler(self.db).execute_sql(None)
File "/opt/secondfunnel/venv/local/lib/python2.7/site-
packages/django/db/models/sql/compiler.py", line 976, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/opt/secondfunnel/venv/local/lib/python2.7/site-
packages/django/db/models/sql/compiler.py", line 782, in execute_sql
cursor.execute(sql, params)
File "/opt/secondfunnel/venv/local/lib/python2.7/site-
packages/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/opt/secondfunnel/venv/local/lib/python2.7/site-
packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/opt/secondfunnel/venv/local/lib/python2.7/site-
packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/secondfunnel/venv/local/lib/python2.7/site-
packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
ProgrammingError: subquery has too many columns
LINE 1: ..." SET "name" = 'foo' WHERE "assets_category"."id" IN (SELECT...
^
}}}
Models:
{{{
class Category(BaseModel):
tiles = models.ManyToManyField(Tile, related_name='categories')
name = models.CharField(max_length=255)
class Tile(Model):
pass
}}}
SQL Query from django.db.connections.queries:
{{{
UPDATE "assets_category" SET "name" = \'foo\' WHERE "assets_category"."id"
IN (SELECT U0."id", COUNT(U2."tile_id") AS "tiles__count" FROM
"assets_category" U0 LEFT OUTER JOIN "assets_category_tiles" U2 ON (
U0."id" = U2."category_id" ) GROUP BY U0."id", U0."name")
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/25171#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/070.c4c398077e4d044728fe969129ae9994%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.