Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

KeyError on psycopg2.extras.register_hstore()

#86

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/extras.py", line 754, in register_hstore
    oid = HstoreAdapter.get_oids(conn_or_curs)
  File "/usr/lib/python2.6/dist-packages/psycopg2/extras.py", line 702, in get_oids
    rv0.append(oids[0])
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

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

State: resolved
Milestone: none
Assigned to: nobody

Activity

  1. Daniele Varrazzo
    Daniele Varrazzo
    • 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.

    January 10th, 2012 @ 01:48 AM

  2. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from hold to resolved

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

    Duplicate of issue #114 hstore adapter.

    September 18th, 2012 @ 08:02 PM

  3. Samuel PHAN
    Samuel PHAN

    Cool, congratz Daniele ! ;-)

    September 19th, 2012 @ 08:31 AM