﻿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
24196	Filtering __in a sliced queryset with a 0 limit raises an unexpected error	Bertrand Bordage	nobody	"{{{AnyModel.objects.filter(pk__in=AnyModel.objects.all()[i:i])}}} (where {{{i}}} is a positive integer) raises a {{{ProgrammingError}}} at least on Django 1.6 and 1.7. It most certainly affects all other Django versions too.

That’s because the ORM avoids converting {{{AnyModel.objects.all()[:0]}}} to a SQL query, while {{{.filter(in___=…)}}} tries to include that inexistant SQL query. The resulting SQL looks like {{{SELECT * FROM any_model WHERE id IN ()}}}, which is invalid.

A working fix is to change [https://github.com/django/django/blob/1.7.3/django/db/models/sql/compiler.py#L91 line 91 of compiler.py] to {{{raise EmptyResultSet}}} instead of {{{return '', ()}}}.
{{{WhereNode.as_sql}}} then [https://github.com/django/django/blob/1.7.3/django/db/models/sql/where.py#L104-111 catches this exception] and notes that it should select nothing. However, I’m not sure that it doesn’t break something else. I can make a pull request with a unit test if you want.

In the meantime, a workaround is to detect whether the inner QuerySet verifies {{{queryset.query.low_mark == queryset.query.high_mark}}}, and do something relevant if it does."	Bug	closed	Database layer (models, ORM)	1.7	Normal	duplicate			Accepted	0	0	0	0	0	0
