﻿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
15049	Using annotation before and after filter gives wrong results	Alex Gaynor	Abhinav Yadav	"Basically:
{{{
qs = Book.objects.values(""name"").annotate(
    n_authors=Count(""authors"")
).filter(
    authors__name__startswith=""Adrian""
).annotate(
    n_authors2=Count(""authors"")
)
}}}

Generates this SQL:

{{{
SELECT
    ""aggregation_regress_book"".""name"",
    COUNT(""aggregation_regress_book_authors"".""author_id"") AS ""n_authors"",
    COUNT(""aggregation_regress_book_authors"".""author_id"") AS ""n_authors2""
FROM
   ""aggregation_regress_book""
   LEFT OUTER JOIN ""aggregation_regress_book_authors"" ON (""aggregation_regress_book"".""id"" = ""aggregation_regress_book_authors"".""book_id"")
   INNER JOIN ""aggregation_regress_book_authors"" T4 ON (""aggregation_regress_book"".""id"" = T4.""book_id"")
   INNER JOIN ""aggregation_regress_author"" T5 ON (T4.""author_id"" = T5.""id"")
WHERE
   T5.""name"" LIKE Adrian% ESCAPE '\'
GROUP BY
    ""aggregation_regress_book"".""name"",
    ""aggregation_regress_book"".""name""
ORDER BY
    ""aggregation_regress_book"".""name"" ASC
}}}

Which uses the same alias for both COUNTs."	Bug	assigned	Database layer (models, ORM)	dev	Normal			mbmohitbagga88@…	Accepted	1	0	0	1	0	0
