#29262: Custom Left Outer Join in Queries
-------------------------------------+-------------------------------------
Reporter: Sassan Haradji | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 2.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ORM Join | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Josh Smeaton):
It's more than likely that I have misinterpreted your request to address
this issue as a demand for core to fix the issue due to language
differences. I apologise.
https://code.djangoproject.com/ticket/26426 (customise joins) and
https://code.djangoproject.com/ticket/25590 (custom join classes) are
almost duplicates, but are much more general than the specific feature of
supporting user-defined left joins. I'd probably argue that
https://code.djangoproject.com/ticket/26426 should be closed, as we have
EXISTS expressions now that can solve that specific problem. Would you
agree Tim?
I think there are really two features that we should try to support.
1. Allow users to join data onto a query with a LEFT JOIN
2. Allow users to add additional conditions onto a JOIN condition
With those two features we'd get pretty far down the line for supporting
most common custom join requirements.
What we'd need to do is come to a consensus on what the right syntax would
look like to make user defined LEFT JOINs possible. I'm not interested in
providing hooks into customising already generated SQL. That would just be
a hack to work around our lack of actual support. That's mostly why the
linked ticket from 10 years ago languished - it was hacked into .extra()
which we're no longer committed to updating.
So what would a decent syntax look like? Should we consider a new queryset
method? Should we use .annotate()?
I'll throw out some ideas:
{{{
MyModel.objects.annotate(joined=Outer(OtherModel.objects.filter(mymodel_id=OuterRef('pk')))
MyModel.objects.outer('othermodel')
MyModel.objects.outer(othermodel=OtherModel.objects.filter(user=request.user))
MyModel.objects.partial_related(othermodel=OtherModel.objects.filter(user=request.user))
}}}
Django has tended to avoid using language that maps too closely to SQL in
the past, though with the addition of more complex expression types that
hasn't been so much of a blocker. I'd be hesitant to add a new queryset
method called outer for that reason though. New classes can map to SQL
because they're not as discoverable and really are for advanced usage.
Increasing the scope of the queryset api with regard to SQL terminology
seems off to me.
This is the kind of question that could probably be asked of the django-
developers mailing list. There are lots of people with opinions there
that'd be relevant to this discussion. In principle though, Django should
definitely support adding left joins to a queryset.
--
Ticket URL: <https://code.djangoproject.com/ticket/29262#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.1498aa57e6e6437b86fbba07f3794932%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.