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

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32879>
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/048.833941f271d1b77860995fb9e975d635%40djangoproject.com.

Reply via email to