﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30863	Queryset __repr__ can overload a database server in some cases	Matt Johnson	nobody	"Consider a model like this:


{{{
class Result(models.Model):
    # A Result object represents someone who took a quiz
    result_id = models.AutoField(primary_key=True, ...)
    quiz = models.ForeignKey(""Quiz"", ...) # assume this boils down to an integer field
    name = models.CharField(...)

    Meta:
        ordering = ['name']
}}}

Assume it has hundreds of millions of records, and no index on the ""name"" column.

Typical usage might be something like
{{{
Result.objects.filter(quiz_id=123)
}}}

Now consider a bug in the usage, like:

{{{
Result.objects.filter(quiz_id=""somestring"") # notice we used a string to filter
}}}
Django will throw an exception (rightfully so).

As part of the usual error reporting process in debug mode, Django may eventually call repr() on the ""base"" queryset (that is essentially Result.objects.all()). 

QuerySet.__repr__ tries to be helpful by printing the first 21 results of the evaluated query. Because the base queryset orders by the un-indexed ""name"" column, this can easily overload the database when it does ""SELECT ... FROM Result ORDER BY name LIMIT 21"" (trying to sort hundreds of millions of rows by an unindexed column)

Even with debug mode turned off, some error reporting tools like Sentry will call repr on the queryset, creating the same problem in production.

I suggest not showing any query data in Queryset.__repr__."	Cleanup/optimization	closed	Database layer (models, ORM)	2.2	Normal	duplicate	queryset repr __repr__		Unreviewed	0	0	0	0	1	0
