Jonathan and Sergey:

I tried to use the `op()` method but for some reason it fails in this 
particular query 
(session.query(Model).filter(Model.description.op('%')('string'))). I get 
the following error:

/home/user/anaconda/envs/gensim/lib/python3.5/site-packages/sqlalchemy/engine/default.py
 
> in do_execute(self, cursor, statement, parameters, context)
>     448
>     449     def do_execute(self, cursor, statement, parameters, 
> context=None):
> --> 450         cursor.execute(statement, parameters)
>     451
>     452     def do_execute_no_params(self, cursor, statement, 
> context=None):
> TypeError: 'dict' object does not support indexing


i tested a similar query with `regexp` and that works correctly, so I'm not 
sure what is different in this case.

Regards

On Thursday, March 31, 2016 at 9:46:34 PM UTC-6, Sergey V. wrote:
>
> Robert, my understanding is that SQLAlchemy knows nothing about the 
> Postgres's `similarity` function - sqlalchemy.func just magically generates 
> the SQL output depending on which member you invoke. Try 
> `func.magic_unicorns()`. So, there's not much to optimize here - it outputs 
> what you give it. See this for details: 
> http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.func
>
> It's up to you to build a query which uses the `%` syntax if you need it. 
> SQLAlchemy's Columns have the `.op()` method for that (
> http://docs.sqlalchemy.org/en/rel_1_0/core/sqlelement.html#sqlalchemy.sql.expression.ColumnElement.op
> )
>
> So this raw sql query: "... WHERE model.description  % 'string' AND 
> similarity(model.description, 'string') > 0.5" becomes
>
>       
> my_query.filter(Model.description.op('%')('string')).filter(func.similarity(Model.description,
>  
> 'string') > 0.5)
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to