﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25171	Can't update queryset after Count annotation	Fraser Harris	nobody	"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"")
}}}"	Bug	new	Database layer (models, ORM)	1.6	Normal		queryset, update, annotate, count		Unreviewed	0	0	0	0	0	0
