#32879: Using annotate and extra in ORM
-------------------------------------+-------------------------------------
     Reporter:  Zerq                 |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  QuerySet.extra       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Zerq:

Old description:

> 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(...)
> }}}

New description:

 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.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32879#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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.314da29fc18cadf14d02898c40507be8%40djangoproject.com.

Reply via email to