I’ve been looking for a way to use tsvector in sqlalchemy (simply like other ones such as INTEGER, etc), but so far it’s not clear to me how to do this. I’ve read that tsvector can be implemented as a type using UserDefinedType. After some attempts I’m getting nowhere, someone has a simple way to do this? thanks
Share
If you want SQLAlchemy to be able to create schemas with the tsvector type and just retrieve the serialized value in queries, this is what you need:
tsvectorworks like a regular type and you can use it within a table definition. (I forgot where I found the snippet, probably on the SQLAlchemy mailing list or wiki.)If you need to actually parse tsvector data, it’s a little more complicated. The latest version’s hstore support might be a good example to follow. However you may find the following snippet useful too. It’s old code that’s been known to work and is written with pyparsing:
Update:
To query the tsvector column, use the
.op()method like this: