On May 23, 2:36 pm, bruno desthuilliers <bruno.desthuilli...@gmail.com> wrote: > > The interaction between > > multiple references to same reverse foreign key relation in single > > queryset is somewhat hard to remember. Could you post the generated > > SQL? > > here you go - reformated for readability: > > SELECT > `blookcore_category`.`id`, > `blookcore_category`.`label` > FROM > `blookcore_category` > LEFT OUTER JOIN `blookcore_classification` > ON (`blookcore_category`.`id` = > `blookcore_classification`.`category_id`) > WHERE > `blookcore_classification`.`blook_id` = 118 > ORDER BY > `blookcore_classification`.`position` ASC > > I don't quite get why it uses a left outer join instead of the inner > join used when not adding the order by clause, but writing a working, > usable ORM is not exactly a piece of cake so I won't complain about > this ;). Anyway: the where clause still makes sure we only get the > relevant categories.
Right you are about the query producing correct results. I should have checked that myself. I guess (again!) that the reason for the LEFT JOIN is this call in sql/ compiler.py:_setup_joins() self.query.promote_alias_chain(joins, self.query.alias_map[joins[0]].join_type == self.query.LOUTER) This seems to promote the join to outer join too aggressively, the promote_alias_chain call does not see that the join is constrained by other conditions already. I don't know if this has any practical effects, nor do I know how easy/hard this would be to fix. It could be as easy as just starting from the first new join generated by the _setup_joins() call, and leaving the pre-existing joins alone. If it is that easy, it should be fixed... - Anssi -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.