On 29 Jan 2013, at 8:50 AM, Alan Etkin <spame...@gmail.com> wrote:
> With trunk, I have tested the behavior of the ~ operator, and doesn't seem 
> consistent to me:
> 
> Here Bruno gives a working example of belongs negation
> https://groups.google.com/d/msg/web2py/fCB9a4K9FqU/GBLwfzNnkjMJ
> 
> But the book uses this example:
> rows = db((~db.person.name=='Alex') | (db.person.id>3)).select()
> 
> This is wat I get in the console (with SQLite/Python 2.7.3)
> 
> >>> q = ~db.auth_user.id.belongs(1,2,3)
> >>> q
> <Query (NOT (auth_user.id IN (1,2,3)))>
> 
> >>> q = ~db.auth_user.id==0
> >>> q
> <Query (auth_user.id DESC = 0)>
> 
> >>> q = ~db.auth_user.id>0
> >>> q
> <Query (auth_user.id DESC > 0)>
> >>> q.select()
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> AttributeError: 'Query' object has no attribute 'select'
> >>> db(q).select()
> Traceback (most recent call last):
> ...
> OperationalError: near "DESC": syntax error
> 
> 
> Note that the second and third query would return an OperationalError (the 
> INVERT operator is used instead of NOT)
> 
> To avoid the error the syntax must be ~(db.auth_user.id==0), with parethesis.
> 
> Is this an problem with the sqlite adapter only?

It's an issue with the precedence of Python operators. ~ has higher precedence 
than comparisons, so the parens are required. Python objects can override the 
functionality of operators, but not their precedence.

A similar caveat applies to the use of | or & in queries.

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to