* status: closed => reopened
* severity: => Normal
* cc: jamey (added)
* resolution: duplicate =>
* component: Uncategorized => Database layer (models, ORM)
* version: 1.0 => 1.3
* easy: => 0
* ui_ux: => 0
* type: => Bug
Comment:
I don't think this is a duplicate of #11329. While both bugs show up when
using `extra(where=...)`, that bug is about column names introduced in
`extra(select=...)`. This bug is about table names introduced with
`select_related()`.
In particular, the patch in the other bug doesn't work around the issue
for code that looks like
{{{
qs.select_related('entry').extra(where=["entry.name='foo'"])
}}}
even though that patch happens to mask this bug if there's a
`.extra(select=...)` in the query as well.
I can confirm that this bug still exists in Django 1.3.1. Also,
`qs.exists()` fails on the same queries.
--
Ticket URL: <https://code.djangoproject.com/ticket/11459#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: j (added)
Comment:
I worked around a similar problem by calling `.annotate()` to force a sub-
select query. I think the problem is specific to MySQL. Can you confirm
that the following works?
{{{#!python
songs = models.Song.objects.select_related('album__genre__genre__id',
'artist__name').extra(where = [where_clause])
t = songs.annotate().count()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/11459#comment:4>
--
Comment (by aaugustin):
Fixed formatting (please use preview).
--
Ticket URL: <https://code.djangoproject.com/ticket/11459#comment:5>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/11459#comment:6>
Comment (by timo):
#18870 is a duplicate.
--
Ticket URL: <https://code.djangoproject.com/ticket/11459#comment:8>
Comment (by akaariai):
Like I commented some time ago in ticket #18870, I don't see any easy
solution here. Not doing select_related is the right choice for .count(),
joins generated by it can't affect the count so it will just slow down the
query. The exception is usage of .extra(), but Django can't know if the
joins that would be generated by select_related are used by extra. So, if
this is going to be fixed there must be some way to tell Django which
select_related() clauses are going to be needed even in case of .count().
Maybe extra(tables=...) could be used for this?
As a temporary workaround it is possible to use a no-op filter (for
example
`Q(album__genre__genre__id__isnull=True)|Q(album__genre__genre__id__isnull=False)`.
This will guarantee that joins are generated even in case of .count().
--
Ticket URL: <https://code.djangoproject.com/ticket/11459#comment:9>
* keywords: => QuerySet.extra
--
Ticket URL: <https://code.djangoproject.com/ticket/11459#comment:10>
* status: new => closed
* resolution: => wontfix
Comment:
We are no longer fixing bugs with `QuerySet.extra()` per
[https://groups.google.com/d/topic/django-
developers/FojuU0syO8Y/discussion discussion on django-developers].
--
Ticket URL: <https://code.djangoproject.com/ticket/11459#comment:11>