SQLAlchemy and Postgresql: to_tsquery ()

Does anyone know how to use the to_tsquery () postgresql function in sqlalchemy? I searched a lot on Google, I did not find anything that I could understand. Please, help.

I hope it is available in the filter function, for example:

session.query(TableName).filter(Table.column_name.to_tsquery(search_string)).all() 

The expected SQL for the above query looks something like this:

 Select column_name from table_name t where t.column_name @@ to_tsquery(:search_string) 
+6
source share
2 answers

For these types of arbitrary queries, you can embed sql directly in your query:

 session.query(TableName).\ filter("t.column_name @@ to_tsquery(:search_string)").\ params(search_string=search_string).all() 

You should also be able to parameterize t.column_name , but you cannot see the documents for this just now.

+8
source

The .op () method allows you to generate SQL for arbitrary statements.

 session.query(TableName).filter( Table.c.column_name.op('@@')(to_tsquery(search_string)) ).all() 
+5
source

Source: https://habr.com/ru/post/909780/


All Articles