Re: How to do equivalent of LEFT JOIN ON in Django (returning all Left rows, when Right item may not exist)
On Sat, Oct 8, 2011 at 8:18 AM, Michal Petrucha <michal.petrucha@ksp.sk> wrote:
> Behind the scenes, Django does a JOIN. Its type (INNER od LEFT OUTER)
> is decided based on whether the left-hand column (your ForeignKey
> field) is allowed to be NULL or not.
the weird part, is that is takes the same choice on reverse
relationships, which isn't necessarily appropriate.
for example, model A has a foreign key to B. when reading A records
with select_related, it makes sense that if the FK is NULLable, it
uses a LEFT OUTER join while if it's not NULLable, use an INNER join.
when I have a B object, it's _really_ handy to have the reverse
relationship set up for me; but what if i want to select those B
objects that doesn't have any A?
this works...
B.objects.filter(a_set__isnull=True)
but only if the FK from A to B has the null=True argument.
and if I want to make the FK from A to B non-NULLable? maybe an A
record doesn't make any sense without B, but B without any A does. so
i don't put (null=True); so in the query above, i get an INNER join
followed by the condition that a.id IS NULL, and the result set is
empty. :-(
the only solution i've found is to make the FK NULLable, even when it
doesn't make sense. I'd love to see a separate reverse_null=True
argument (by default reverse_null==null) on the FK, that would make
the reverse join a LEFT OUTER one while still prohibiting an A record
without B.
am i missing something?
--
Javier
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home