#28297: Same queryset result in two different queries on ORM
-------------------------------------+-------------------------------------
     Reporter:  Marcus Renno         |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  join, annotation, F  |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Marcus Renno):

 Tom, I agree with you that it should be the second query and I believe
 that the feature should be supported. It's just a bug and we will fix it
 ;)

 I'll try to work on this issue when I get home. I think you did a
 fantastic job narrowing down the problem and it's close to be solved.


 Replying to [comment:20 Tom]:
 > I've dug into this a fair bit and I'm sorry to say I'm stuck. I also
 think there is a pretty big bug lurking here that is out of my league. Or
 maybe it's a feature we don't support?
 >
 > As far as I can tell, if you annotate over foreign keys as well as m2m:
 >
 > {{{
 > Publisher.objects
 >    .annotate(num_books=Count('book', distinct=True))
 >    .filter(book__rating__gt=3.0)
 >    .annotate(num_rated=Count('book', distinct=True))
 >    .filter(num_books=F('num_rated'))
 >
 > }}}
 >
 > Then the resulting SQL will have the following `HAVING` clause:
 >
 > `HAVING COUNT(DISTINCT "publisher"."id") = (COUNT(DISTINCT
 "publisher"."id"))`
 >
 > Am I correct in thinking that this should be:
 >
 > `HAVING COUNT(DISTINCT "publisher"."id") = (COUNT(DISTINCT "T3"."id"))`,
 where `T3` is the correct join?
 >
 > In any case, either the generated query is incorrect or the
 documentation needs to be updated.
 >
 > I think, at least in the case of a m2m join, one of the issues are that
 there seems to be no way to resolve which alias is used by two identical
 annotations which are affected by a previous `.filter()` in the
 `query.join` method.

--
Ticket URL: <https://code.djangoproject.com/ticket/28297#comment:22>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/069.7d20d73cf17dea142b4b39b9d8af6ed5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to