Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hstore with cursor_factory=psycopg2.extras.RealDictCursor Errors #189

Closed
bgcgarlipp opened this issue Oct 21, 2016 · 1 comment
Closed

Comments

@bgcgarlipp
Copy link

Hi

When intializing a new connection with cursor_factory=psycopg2.extras.RealDictCursor on a databse with hstore extension on, you get an error as follows. This is due to a dict being returned as result and not list.

Traceback (most recent call last):
File "wsclient.py", line 134, in
loop.run_until_complete(db.init_db())
File "/usr/lib/python3.5/asyncio/base_events.py", line 337, in run_until_complete
return future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "/home/bernhardt/git_work/qs_switch/QS_Workflow/client/async_quantdb.py", line 25, in init_db
cursor_factory=psycopg2.extras.RealDictCursor)
File "/home/bernhardt/git_work/qs_switch/QS_Workflow/pyenv/lib/python3.5/site-packages/aiopg/utils.py", line 57, in await
resp = yield from self._coro
File "/home/bernhardt/git_work/qs_switch/QS_Workflow/pyenv/lib/python3.5/site-packages/aiopg/pool.py", line 45, in _create_pool
yield from pool._fill_free_pool(False)
File "/home/bernhardt/git_work/qs_switch/QS_Workflow/pyenv/lib/python3.5/site-packages/aiopg/pool.py", line 188, in _fill_free_pool
**self._conn_kwargs)
File "/home/bernhardt/git_work/qs_switch/QS_Workflow/pyenv/lib/python3.5/site-packages/aiopg/utils.py", line 52, in iter
resp = yield from self._coro
File "/home/bernhardt/git_work/qs_switch/QS_Workflow/pyenv/lib/python3.5/site-packages/aiopg/connection.py", line 78, in _connect
oids = yield from _enable_hstore(conn)
File "/home/bernhardt/git_work/qs_switch/QS_Workflow/pyenv/lib/python3.5/site-packages/aiopg/connection.py", line 37, in _enable_hstore
rv0.append(oids[0])
KeyError: 0

How to reproduce:
install

sudo apt-get install postgresql-contrib
create hstore extension on database

CREATE EXTENSION hstore;

create connection to datbase using aiopg, we created a pool, but as it calls connect function inside normal conection should fail aswell
self._pool = await aiopg.create_pool(host=self.settings.DATABASE_HOST, database=self.settings.DATABASE_NAME, user=self.settings.DATABASE_USER, password=self.settings.DATABASE_PASS, port=self.settings.DATABASE_PORT, cursor_factory=psycopg2.extras.RealDictCursor)

Suggested solution:
Change "def _enable_hstore(conn):" in aiopg.connection
from

@asyncio.coroutine
def _enable_hstore(conn):
    cur = yield from conn.cursor()
    yield from cur.execute("""\
        SELECT t.oid, typarray
        FROM pg_type t JOIN pg_namespace ns
            ON typnamespace = ns.oid
        WHERE typname = 'hstore';
        """)
    rv0, rv1 = [], []
    for oids in (yield from cur.fetchall()):
        rv0.append(oids[0])
        rv1.append(oids[1])
    cur.close()
    return tuple(rv0), tuple(rv1)

To

@asyncio.coroutine
def _enable_hstore(conn):
    cur = yield from conn.cursor()
    yield from cur.execute("""\
        SELECT t.oid, typarray
        FROM pg_type t JOIN pg_namespace ns
            ON typnamespace = ns.oid
        WHERE typname = 'hstore';
        """)
    rv0, rv1 = [], []
    for oids in (yield from cur.fetchall()):
        if type(oids) is list:
            rv0.append(oids[0])
            rv1.append(oids[1])
        else:
            rv0.append(oids['oid'])
            rv1.append(oids['typarray'])
    cur.close()
    return tuple(rv0), tuple(rv1)

Regards

@jettify
Copy link
Member

jettify commented Oct 21, 2016

@bgcgarlipp thanks for report! would you like to create PR?

bgcgarlipp added a commit to bgcgarlipp/aiopg that referenced this issue Oct 22, 2016
This is my proposed solution for issue aio-libs#189.
It sesolves the problem where the query result is returned as a dict and not a list due to the cursor_factory being a RelDictRow
angaz added a commit to angaz/aiopg that referenced this issue Oct 24, 2018
@vir-mir vir-mir closed this as completed in 57b5d55 Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants