--
Ticket URL: <https://code.djangoproject.com/ticket/18378>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_docs: => 0
* needs_tests: => 0
* needs_better_patch: => 0
Comment:
In the description, I changed the Django query and forgot to paste the new
SQL: the last "foo" in the SQL should of course be a "goo", and the
COUNT(foo_model2.id) should be COUNT(foo_model2.goo).
--
Ticket URL: <https://code.djangoproject.com/ticket/18378#comment:1>
Comment (by anonymous):
Replying to [comment:1 anonymous]:
> In the description, I changed the Django query and forgot to paste the
new SQL: the last "foo" in the SQL should of course be a "goo", and the
COUNT(foo_model2.id) should be COUNT(foo_model2.goo).
I mean, COUNT(foo_model2.foo)
--
Ticket URL: <https://code.djangoproject.com/ticket/18378#comment:2>
* stage: Unreviewed => Accepted
Comment:
I tested this and the query works if the name column is added just into
the GROUP BY clause, even if it isn't in the SELECT list.
I can't see any sane reason for allowing the query only if the column is
in the select list. I am tempted to call this a bug in MySQL and just
wontfix this. But maybe I will resist that feeling...
--
Ticket URL: <https://code.djangoproject.com/ticket/18378#comment:3>
Comment (by anonymous):
Replying to [comment:3 akaariai]:
> I tested this and the query works if the name column is added just into
the GROUP BY clause, even if it isn't in the SELECT list.
That's a good point. Can you think of some way to make Django put the
column in the GROUP BY clause?
--
Ticket URL: <https://code.djangoproject.com/ticket/18378#comment:4>
Comment (by anonymous):
I think this should really be fixed, be it as it may an SQL bug.
In the meantime, I'm using the following workaround:
{{{#!python
ids = [x[0] for x in
Model2.objects.annotate(bar=Count('foo')).filter(Q(bar__gt=0)|Q(model1__name='goo')).values('id',
'model1__name')]
Model2.objects.filter(id__in=ids)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18378#comment:5>
Comment (by joseph.helfer@…):
Replying to [comment:5 anonymous]:
> I think this should really be fixed, be it as it may an SQL bug.
>
> In the meantime, I'm using the following workaround:
> {{{#!python
> ids = [x[0] for x in
Model2.objects.annotate(bar=Count('foo')).filter(Q(bar__gt=0)|Q(model1__name='goo')).values('id',
'model1__name')]
> Model2.objects.filter(id__in=ids)
> }}}
I mean,
{{{#!python
ids = [x[0] for x in
Model2.objects.annotate(bar=Count('foo')).filter(Q(bar__gt=0)|Q(model1__name='goo')).values_list('id',
'model1__name')]
Model2.objects.filter(id__in=ids)
}}}
(`values_list`, not `values`)
--
Ticket URL: <https://code.djangoproject.com/ticket/18378#comment:6>
* status: new => closed
* resolution: => worksforme
Comment:
I looked into this and couldn't replicate the problem on the {{{master}}}
branch. I assume the bug has been fixed in the 6 years since being
reported.
I used the {{{Book}}} model in {{{tests.annotations.models}}} to write
this query:
{{{#!python
qs = Book.objects.annotate(author_count=Count('authors')).filter(
Q(author_count__gt=0) | Q(publisher__name='Sams')
)
}}}
This gave reasonable results when running the test on mysql.
--
Ticket URL: <https://code.djangoproject.com/ticket/18378#comment:7>