﻿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
27727	Exclude with many condition in the same related field's fields	Mounir	nobody	"When trying to exclude with many conditions in the same related field the generated query (tested in Postgresql) will not be the desired one.
E.g. If I have a model Student and a model Teacher, Student have a ForeignKey to Teacher model.


{{{
Teacher.objects.exclude(student__age=20, student__note__lt=5).query
}}}

 Which will results in this query:

{{{
SELECT ""teachers_teacher"".""id"", ""teachers_teacher"".""name"" FROM ""teachers_teacher"" WHERE NOT (""teachers_teacher"".""id"" IN (SELECT U1.""teacher_id"" AS Col1 FROM ""teachers_student"" U1 WHERE U1.""age"" = 20) AND ""teachers_teacher"".""id"" IN (SELECT U1.""teacher_id"" AS Col1 FROM ""teachers_student"" U1 WHERE U1.""note"" < 5))
}}}

I think the right SQL will be the following:

{{{
SELECT ""teachers_teacher"".""id"", ""teachers_teacher"".""name"" FROM ""teachers_teacher"" WHERE NOT (""teachers_teacher"".""id"" IN (SELECT U1.""teacher_id"" AS Col1 FROM ""teachers_student"" U1 WHERE U1.""age"" = 20 AND U1.""note"" < 5))
}}}

Even if the fix will be need a lot of work, I think it's better to mention this in the exclude documentation.
"	Bug	closed	Database layer (models, ORM)	1.10	Normal	duplicate			Unreviewed	0	0	0	0	0	0
