Skip to content
Advertisement

Create a Full Text Search index with SQLAlchemy on PostgreSQL

I need to create a PostgreSQL Full Text Search index in Python with SQLAlchemy. Here’s what I want in SQL:

JavaScript

Now how do I do the second part with SQLAlchemy when using the ORM:

JavaScript

Advertisement

Answer

You could create index using Index in __table_args__. Also I use a function to create ts_vector to make it more tidy and reusable if more than one field is required. Something like below:

JavaScript

Update: A sample query using index (corrected based on comments):

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement