On Sep 21, 2010, at 8:12 AM, Michael Hipp wrote:

> On 9/20/2010 10:54 AM, Michael Hipp wrote:
>> On 9/20/2010 10:09 AM, Michael Hipp wrote:
>>> On 9/20/2010 9:38 AM, Michael Hipp wrote:
>>>> Scratch that ... found this message:
>>>> http://www.mail-archive.com/[email protected]/msg18598.html
>>>> which says I should be able to do a 'SIMILAR TO' construct which is perhaps
>>>> somewhat more lightweight than a full regexp.
>>> 
>>> Can someone show me what I'm doing wrong here.
>>> 
>>> letter = 'A[0-9]+'
>>> q = self.session.query(Car).filter_by(hist=False) \
>>> .filter(Car.lane.op('SIMILAR TO') (letter)) \
>>> .order_by(Car.lane)
>>> 
>>> I'm trying to match something that looks like 'A100'. But it produces a 
>>> syntax
>>> error:
>>> 
>>> OperationalError: (OperationalError) near "SIMILAR": syntax error
>>> ...snip...
>>> WHERE cars.hist = ? AND (cars.lane SIMILAR TO ?) ORDER BY cars.lane' (False,
>>> 'A[0-9]+')
>> 
>> Strange. I couldn't see what was actually wrong with that SQL, so I ran it
>> directly against pg and it works fine. Is it possibly a quoting problem?
> 
> Can anyone offer me some suggestions here? Is this a bug?

I'm not sure how the ? is being used for a bind param.  the psycopg2 dialect 
uses %(name)s format.   SIMILAR TO works fine and you can see %(name)s format 
is used:

from sqlalchemy import *
from sqlalchemy.sql import column

e = create_engine('postgresql://scott:ti...@localhost/test', echo=True)

e.execute(select([literal_column("'lane'").op('SIMILAR TO')('car')]))

2010-09-21 08:19:49,339 INFO sqlalchemy.engine.base.Engine.0x...1570 select 
version()
2010-09-21 08:19:49,339 INFO sqlalchemy.engine.base.Engine.0x...1570 {}
2010-09-21 08:19:49,341 INFO sqlalchemy.engine.base.Engine.0x...1570 select 
current_schema()
2010-09-21 08:19:49,341 INFO sqlalchemy.engine.base.Engine.0x...1570 {}
2010-09-21 08:19:49,343 INFO sqlalchemy.engine.base.Engine.0x...1570 SELECT 
'lane' SIMILAR TO %('lane'_1)s AS anon_1
2010-09-21 08:19:49,343 INFO sqlalchemy.engine.base.Engine.0x...1570 
{"'lane'_1": 'car'}

I get the same result with psycopg2 2.0.12 and 2.2.2 .





-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to