#16766: Senseless query for complex relations
---------------------------+----------------------------------------------
 Reporter:  theaspect@…    |          Owner:  nobody
     Type:  Uncategorized  |         Status:  new
Milestone:                 |      Component:  Database layer (models, ORM)
  Version:  1.3            |       Severity:  Normal
 Keywords:                 |   Triage Stage:  Unreviewed
Has patch:  0              |  Easy pickings:  0
    UI/UX:  0              |
---------------------------+----------------------------------------------
 Here my models
 {{{
 class Group(Model):
     pass

 class SubGroup(Model):
     group = ForeignKey(Group)

 class User(Model):
     group = (Group)
     subgroup = (SubGroup, null = true, blank = true)
 }}}

 so, I want all Users whose subgroup's group does not equal group, note
 null option for user
 {{{
 User.objects.except('subgroup__group' = F('group'))
 }}}

 in query we get all users with subgroup = null, and in iteration, all
 reference to user.subgroup.group will throw exception, because, obviously,
 subgroup is None

 here sql that I've got from django
 {{{
 SELECT * FROM `user` LEFT OUTER JOIN `subgroup` ON (`user`.`subgroup_id` =
 `subgroup`.`id`) WHERE NOT ((`subgroup`.`group_id` =  `user`.`group_id`
 AND NOT (`subgroup`.`id` IS NULL)))
 }}}

 here what i'm expect

 {{{

 SELECT * FROM user u
 join group g on g.id = u.group_id
 left join subgroup s on u.subgroup_id = s.id and s.group_id = g.id
 where not (s.group_id = u.group_id)

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/16766>
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 post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to