#28297: Same queryset result in two different queries on ORM -------------------------------------+------------------------------------- Reporter: Marcus Renno | Owner: Tom Type: Bug | Status: assigned 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 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 do: {{{ 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:20> 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.d5a510237f88c7a8c0eabac9e4ad6793%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.