﻿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
1535	[patch] ManyToMany query problems on magic-removal	Russell Cloran <russell@…>	Adrian Holovaty	"Hi,

When querying a model where one of the query terms includes a ManyToMany relation, some data which should appear in the result doesn't. This occurs if the ManyToMany relation does not have any data. To be more clear, here is an example:

{{{
class Issue:
    ...
    cc = models.ManyToManyField(User, blank=True)
    client = models.ForeignKey(User)
    ....

>>> Issue.objects.filter(Q(client=u.id))
[Issue 1, Issue 7]
>>> Issue.objects.filter(Q(cc__id__exact=u.id))
[Issue 7, Issue 8]
>>> Issue.objects.filter(Q(cc__id__exact=u.id) | Q(client=u.id))
[Issue 7, Issue 8]
}}}

The result for the last query should include ""Issue 1"", as far as I can tell. The following patch solves the problem, but it was derived by watching which queries happened and hacking the code to make my case work, so I don't know what it breaks :)

{{{
===================================================================
--- django/db/models/query.py   (revision 2546)
+++ django/db/models/query.py   (working copy)
@@ -769,7 +769,7 @@
     if intermediate_table:
         joins[backend.quote_name(current_table)] = (
             backend.quote_name(intermediate_table),
-            ""INNER JOIN"",
+            ""LEFT OUTER JOIN"",
             ""%s.%s = %s.%s"" % \
                 (backend.quote_name(table),
                 backend.quote_name(current_opts.pk.column),
}}}

"	defect	closed	Database layer (models, ORM)	magic-removal	normal	fixed		mir@…	Unreviewed	1	0	0	0	0	0
