4

I am attempting to implement this code https://gist.github.com/1859653 that allows sqlalchemy to interact with an hstore column.

Its mentioned in that gist's comments the need to run the psycopg2.extras.register_hstore. When and were should this function be run? If i do:

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

heroku errors with 'too many connections'

there is also mention of using pghstore (http://pypi.python.org/pypi/pghstore) instead of psycopg2 but it gives no indication how to set it up.

Also, I'm wondering if the use of hstore indexes is supported in this add-on code.

1
  • Armin has responded that the best place for this code may be in get_engine function of flask-alchemy. Does anyone know how i would access this to put in the register_hstore function? Commented Sep 24, 2012 at 15:37

2 Answers 2

7

Since SQLAlchemy 0.8, psycopg2 connector for PostgreSQL (which is default) will register hstore extension by default and the apply_driver_hacks workaround is not needed anymore.

Also, SQLAlchemy 0.8+ has builtin HSTORE type support.

Sign up to request clarification or add additional context in comments.

Comments

4

Try this:

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)

Also see:

2 Comments

i got this working and it appears to be working great. thanks a million!
FWIW my drivername was "postgresql", not "postgres"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.