This project is archived and is in readonly mode.

#86 ✓resolved
Samuel PHAN

KeyError on psycopg2.extras.register_hstore()

Reported by Samuel PHAN | December 22nd, 2011 @ 10:17 AM

I have an error when registering hstore on a connection with psycopg2.extras.RealDictConnection.

>>> conn = psycopg2.connect('host=localhost dbname=dev', connection_factory=psycopg2.extras.RealDictConnection)
>>> psycopg2.extras.register_hstore(conn, globally=False, unicode=True, oid=None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/psycopg2/", line 754, in register_hstore
    oid = HstoreAdapter.get_oids(conn_or_curs)
  File "/usr/lib/python2.6/dist-packages/psycopg2/", line 702, in get_oids
KeyError: 0

It works fine when using psycopg2.extras.DictConnection.

>>> conn = psycopg2.connect('host=localhost dbname=dev', connection_factory=psycopg2.extras.DictConnection)
>>> psycopg2.extras.register_hstore(conn, globally=False, unicode=True, oid=None)

psycopg2: 2.4.4
Postgres: 9.1.2
OS: Ubuntu 10.10

Comments and changes to this ticket

  • Daniele Varrazzo

    Daniele Varrazzo January 10th, 2012 @ 01:48 AM

    • State changed from “new” to “hold”

    As it says on the tin, RealDictCursor is "extremely specialized and does not allow the normal access (using integer indices) to fetched data". It basically violates the DBAPI interface. For this reason I'm against fixing it: register_hstore() (but also register_composite() and probably tpc_recover() too) just assumes the cursor is DBAPI-valid, and accepting the possibility it isn't just makes a mess. For example, if you pass a cursor to register_hstore(), it can't be used for querying, but its connection must be obtained and a plain cursor built out of it - and I'm not even sure cursor_factory is honoured by all the connection subclasses.

    There are other ways to register hstore and use it from a RealDictCursor: use a regular cursor and register it on the connection, or use a regular connection and register it globally, or use a regular connection to get the hstore oid and register it without the internal query, as explained in the docs.

  • Daniele Varrazzo

    Daniele Varrazzo September 18th, 2012 @ 08:02 PM

    • State changed from “hold” to “resolved”

    This issue was fixed in my devel. To be released in 2.4.6.

    Duplicate of issue #114.

  • Samuel PHAN

    Samuel PHAN September 19th, 2012 @ 08:31 AM

    Cool, congratz Daniele ! ;-)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="">the new tracker</a>.
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins