On Thu, 2009-01-08 at 10:58 +0100, Christopher Mutel wrote:
> Hello all-
> I recently filed a bug about incorrect SQL generation, and Malcom
> Tredinnick said that the example SQL I provided couldn't be correct,
> because there was an extra LIMIT clause that shouldn't be there. After
> poking around for a bit, I realized that everytime I was executing my
> query in the IPython shell, it was appending a LIMIT 21 to the SQL,
> but this limit clause wasn't being appended if the query was part of a
> regular python process. Both queries are run against SQLite 3.

Aah .. I understand what's going on now! :-)

You were printing (or, at least, trying to print) the repr() of the
queryset. To avoid people accidentally trying to retrieve and print a
million results, we (well, I) changed that to only retrieve and print
the first 20 results and print "remainder truncated" if there were more.
This is achieved by limiting the query to 21 results (if there are 21
results there are more than 20, so we print the "truncated" message).
That only happens in the  __repr__() -- i.e. it's only for diagnostic
printing. No normal user code has this limit included automatically, so
you happily create a queryset that iterates over a million results.

It hadn't occurred to me when reading that ticket that you were pulling
the query out of django.db.connection.queries, but that makes sense now
that I see your code. I, almost invariably, use
"queryset.query.as_sql()" to view the query that the queryset is
creating and that wouldn't show the limit.

So, you aren't losing your mind (and, luckily, neither am I). The SQL
limit you're seeing isn't the direct representation of the queryset.
It's a result of trying to print the repr() of the queryset. Not related
to IPython or anything like that. Just a safeguard against accidental
repr() printing (after hearing people like Google, NASA and even a
semi-well-known media organisation in Kansas complain about making that
blunder, I broke down and added it a couple of months ago).

Thanks for following this up a bit more. Hopefully next time I see a
strange "LIMIT 21" on something, I'll remember how it might occur.

In any case, your example was clear enough that I could repeat the
problem and, as you have no doubt seen, it was actually exposing a
different and more significant problem which has now been fixed.


You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to