﻿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
10670	Using a QuerySet in a filter expression can cause later evaluation of the QuerySet to raise ProgrammingError	Ben Anhalt	Alex Gaynor	"Adding the following test case to tests/modeltests/aggregation/models.py:

{{{
>>> publishers = Publisher.objects.filter(id__in=(1,2))
>>> publishers
[<Publisher: Apress>, <Publisher: Sams>]

>>> publishers = publishers.annotate(n_books=models.Count('book'))
>>> publishers[0].n_books
2

>>> publishers # Here it is OK
[<Publisher: Apress>, <Publisher: Sams>]

>>> books = Book.objects.filter(publisher__in=publishers)
>>> books
[<Book: Sams Teach Yourself Django in 24 Hours>, <Book: Practical Django Projects>, <Book: The Definitive Guide to Django: Web Development Done Right>]

>>> publishers # Here it fails.
[<Publisher: Apress>, <Publisher: Sams>]
}}}

results in:
{{{
Failed example:
    publishers # Here it fails.
Exception raised:
    Traceback (most recent call last):
      File ""/usr/lib/python2.5/site-packages/django/test/_doctest.py"", line 1267, in __run
        compileflags, 1) in test.globs
      File ""<doctest modeltests.aggregation.models.__test__.BUG[8]>"", line 1, in <module>
        publishers # Here it fails.
      File ""/usr/lib/python2.5/site-packages/django/db/models/query.py"", line 55, in __repr__
        data = list(self[:REPR_OUTPUT_SIZE + 1])
      File ""/usr/lib/python2.5/site-packages/django/db/models/query.py"", line 70, in __len__
        self._result_cache.extend(list(self._iter))
      File ""/usr/lib/python2.5/site-packages/django/db/models/query.py"", line 193, in iterator
        for row in self.query.results_iter():
      File ""/usr/lib/python2.5/site-packages/django/db/models/sql/query.py"", line 262, in results_iter
        for rows in self.execute_sql(MULTI):
      File ""/usr/lib/python2.5/site-packages/django/db/models/sql/query.py"", line 2285, in execute_sql
        cursor.execute(sql, params)
    ProgrammingError: missing FROM-clause entry for table ""U1""
    LINE 1: ...ame"", ""aggregation_publisher"".""num_awards"", COUNT(""U1"".""id"")...
}}}

I'm using postgres."		closed	Database layer (models, ORM)	dev		fixed			Ready for checkin	1	0	0	0	0	0
