How to use Postgres' Hstore column with sqlalchemy bulb?

I am trying to implement this code https://gist.github.com/1859653 which allows sqlalchemy to interact with the hstore column.

The comments mentioned in this document about the need to run psycopg2.extras.register_hstore. When was this feature launched? If I do:

@app.before_request def reg_hstore() : register_hstore(db.engine.raw_connection(), True) 

mistakes of the hero with "too many connections"

it also mentions the use of pghstore (http://pypi.python.org/pypi/pghstore) instead of psycopg2, but it does not give instructions on how to configure it.

Also, I am wondering if the use of hstore indexes is supported in this additional code.

+4
source share
2 answers

Try the following:

 from flaskext.sqlalchemy import SQLAlchemy import psycopg2 import psycopg2.extras class _SQLAlchemy(SQLAlchemy): def apply_driver_hacks(self, app, info, options): """This method adds option to support hstore on psycopg2""" if info.drivername == "postgres": def _connect(): conn = psycopg2.connect(user=info.username, host=info.host, port=info.port, dbname=info.database, password=info.password) psycopg2.extras.register_hstore(conn) return conn options["creator"] = _connect SQLAlchemy.apply_driver_hacks(self, app, info, options) 

See also:

+4
source

Since SQLAlchemy 0.8, psycopg2 for PostgreSQL (by default) will register the hstore extension by default , and the apply_driver_hacks no longer needed.

In addition, SQLAlchemy 0.8+ has built-in support for the HSTORE type.

+5
source

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


All Articles