Belongs with empty list fails on postgres

330 views
Skip to first unread message

Marin Pranjić

unread,
Nov 6, 2012, 11:01:08 AM11/6/12
to web...@googlegroups.com
The issue is that SQLite allows to do:
something = []
db
(db.tablename.fieldname.belongs(something)).select()

However this fails on postgres (not sure what about the others).

I am using SQLite for development so I don't notice the error until I move the code into production.
Not only I have error tickets in production (which should not happen), my code ends up with lots of if-else lines and I don't like it either.

I think DAL should be smarter. Now it passes a query to db and postgres raises a syntax error.
What do you think?


Niphlod

unread,
Nov 6, 2012, 11:10:33 AM11/6/12
to web...@googlegroups.com
uhm.
elif second==[] or second==():
            return '(1=0)'
why this is not reached ?

what is printed when you use db(db.tablename.fieldname.belongs([]))._select() ?

Marin Pranjić

unread,
Nov 6, 2012, 11:27:04 AM11/6/12
to web...@googlegroups.com
I tried this:
db(db.auth_user.id.belongs())._select(db.auth_user.id)

And it prints:
'SELECT  auth_user.id FROM auth_user WHERE (1=0);'


So, it works as expected.
My question is invalid because I have something else in code:


db.tablename.fieldname.belongs(set())

Which is not working:

'SELECT  auth_user.id FROM auth_user WHERE (auth_user.id IN ());'


So the issue is with set, not a list.
Sorry :)

Niphlod

unread,
Nov 6, 2012, 11:48:12 AM11/6/12
to web...@googlegroups.com
ok then post a bug so it doesn't get lost.
PS: I remember asking for the same thing and patching DAL, but it was for empty lists indeed.

Marin Pranjić

unread,
Nov 6, 2012, 12:01:59 PM11/6/12
to web...@googlegroups.com
Done: http://code.google.com/p/web2py/issues/detail?id=1147


Is there a reason for
elif second==[] or second==()

while it could easily be
elif not second
?

This should fix it.

Niphlod

unread,
Nov 6, 2012, 12:06:22 PM11/6/12
to web...@googlegroups.com
really don't know (but probably to ckeck for nulls)

the complete code is

def BELONGS(self, first, second):
        if isinstance(second, str):
            return '(%s IN (%s))' % (self.expand(first), second[:-1])

        elif second==[] or second==():
            return '(1=0)'
        items = ','.join(self.expand(item, first.type) for item in second)
        return '(%s IN (%s))' % (self.expand(first), items)

Cliff Kachinske

unread,
Nov 8, 2012, 12:29:22 PM11/8/12
to web...@googlegroups.com
I've been working around this Postgres/psycopg2 "feature" for ages.

Something like this:

query = db.sometable.somefield==somevalue
avoid = [id1, id2, id3] #could be an empty list
if len(avoid):
    query &= ~(db.sometable.id.belongs(avoid))

Richard Vézina

unread,
Apr 12, 2013, 12:39:14 PM4/12/13
to web2py-users
Hello,

I fall on "(1=0)" with this query :

There is these belongs :

db(... & (db.table.field.belongs(('item',))).select(...)

Richard


--
 
 
 

Richard Vézina

unread,
Apr 12, 2013, 12:41:13 PM4/12/13
to web2py-users
Seems I can work around this like that :

db(... & (db.table.field.belongs(('item', ''))).select(...)

Richard
Reply all
Reply to author
Forward
0 new messages