Still a bug with OR lookups?

4 views
Skip to first unread message

Julien

unread,
May 30, 2008, 2:57:25 AM5/30/08
to Django users
Hi,

A while ago I stumbled on a bug with OR lookups, but Malcolm had
apparently fixed it in the queryset-refactor branch [1]. However I've
just got a similar issue. Is that still a bug or am I mis-using it?

Here's my model:

class Contact(models.Model):
requesting_user = models.ForeignKey(User,
related_name='contacts_has_requested')
requested_user = models.ForeignKey(User,
related_name='contacts_was_requested')
is_blocked = models.BooleanField(default=False)

Now the test code:

q1 = Q(contacts_was_requested__is_blocked=False,
contacts_was_requested__requesting_user=self)
p1=User.objects.filter(q1)
=> returns: QuerySet: []

q2 = Q(contacts_has_requested__is_blocked=False,
contacts_has_requested__requested_user=self)
p2=User.objects.filter(q2)
=> returns: QuerySet: [<User: user_5>]

p3=User.objects.filter(q1 | q2)
=> returns: QuerySet: []

Shouldn't the '|' sign do a union of the two intermediary querysets?

Am I missing something?

Thanks a lot,

Julien

[1]
http://groups.google.com/group/django-users/browse_thread/thread/a4679cb9a42c6ba1/1e76718aa0097628?lnk=gst&q=julien+q+malcolm+bug#1e76718aa0097628

omat

unread,
May 30, 2008, 5:12:53 AM5/30/08
to Django users
Hi Julien,

I think the OR'ing of querysets (unions) is bogus.

When I have a queryset that has some 'extra' parameter that builds a
non-exclusive join SQL, and try to merge it with an other queryset,
the table names are not handled correctly. Sometimes some tables are
ignored and sometimes they are injected into SQL's FROM multiple times
resulting in a DB error.

I tried to explain the problem here, last week:
http://groups.google.com/group/django-users/browse_thread/thread/48cd89cd0ab099e1/

And I found a ticket that can be related to the issues:
http://code.djangoproject.com/ticket/7277

I tried to track down the problem in the django.db.query and solve my
problems partially by monkey patching but I am not comfortable at all
and looking forward for the official resolution.


--
omat
> [1]http://groups.google.com/group/django-users/browse_thread/thread/a467...

omat

unread,
May 30, 2008, 5:21:52 AM5/30/08
to Django users
And, I think your problem is the same that makes "union with an empty
queryset always result in an empty queryset" bug:

User.objects.filter(Q) | User.objects.none()
=> always returns an empty queryset.


--
omat



On May 30, 12:12 pm, omat <o...@gezgin.com> wrote:
> Hi Julien,
>
> I think the OR'ing of querysets (unions) is bogus.
>
> When I have a queryset that has some 'extra' parameter that builds a
> non-exclusive join SQL, and try to merge it with an other queryset,
> the table names are not handled correctly. Sometimes some tables are
> ignored and sometimes they are injected into SQL's FROM multiple times
> resulting in a DB error.
>
> I tried to explain the problem here, last week:http://groups.google.com/group/django-users/browse_thread/thread/48cd...

Julien

unread,
May 30, 2008, 8:01:56 PM5/30/08
to Django users
Thanks for the info Omat.

> And, I think your problem is the same that makes "union with an empty
> queryset always result in an empty queryset" bug:
>
> User.objects.filter(Q) | User.objects.none()
> => always returns an empty queryset.
>

Has this in particular been reported?

Julien
Reply all
Reply to author
Forward
0 new messages