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),