﻿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
32879	Using annotate and extra in ORM	Zerq	nobody	"In simple example:
{{{
class Foo(models.Model):
     pass
class Bar(models.Model):
     baz = models.ForeignKey('Foo', on_delete=models.PROTECT)
}}}
I want to count reverse relation count and then select_for_update:
{{{
Foo.objects.annotate(Count('bar')).select_for_update().get(...)
}}}
but this does not work: django.db.utils.NotSupportedError: FOR UPDATE is not allowed with GROUP BY clause

To circumvent this problem I used:
{{{
Foo.extra(select={'bar_count': 'SELECT COUNT(*) FROM ""app_bar"" U0 WHERE U0.""baz_id"" = ""app_foo"".""id""'}).select_for_update().get(...)
}}}

I file this issue to show usage of extra, as asked in documentation.
"	Uncategorized	new	Database layer (models, ORM)	3.2	Normal		QuerySet.extra		Unreviewed	0	0	0	0	0	0
