#29262: Custom Left Outer Join in Queries
-------------------------------------+-------------------------------------
     Reporter:  Sassan Haradji       |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  ORM Join             |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Hugo Maingonnat):

 I also need a way to generate a query with custom a join:

 {{{
 SELECT "main_table"."id" FROM "main_table" LEFT JOIN (SELECT
 distinct(main_id) FROM through_table WHERE other_id = 14) as foo ON
 foo.main_id=main_table.id ORDER BY "main_table"."id" DESC LIMIT 1;
 }}}

 The main queryset is generated by the view, and the left join subquery is
 a custom django-filter. Using a `id__in` would work but I have to repeat
 the ORDER BY DESC and LIMIT 1 in the subquery (otherwise it gets really
 ineficient) and ideally the filter should be independant from the initial
 queryset (so it does not know the ordering and limit). Also django-filter
 needs to keep a queryset so I cannot generate a RawQueryset (I am not even
 sure this is doable).

 Having a `join` keyword in the extra function would have unlocked me.

 The FilteredRelation object is only limited to a condition (q object) and
 not a full SELECT query. If it could take a RawSQL or a queryset it would
 be perfect.

 So it looks like I cannot generate a quite simple query using any of the
 django function, a bit frustrating :(

 Like @Alexandr Artemyev I found out the CustomJoin trick but it looks very
 brittle.

 Hope my feedback will help understanding the issue :)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29262#comment:18>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.3ae24a83b63ae19ecac4b3b3f1f50438%40djangoproject.com.

Reply via email to