This project is archived and is in readonly mode.

#85 ✓invalid
Samuel PHAN

Row type is not the expected one, depending on the used factory connection

Reported by Samuel PHAN | December 21st, 2011 @ 10:12 AM

Depending on the connection factory, the row type, when returned in an iterator way ( is not the right one, or is bugged.

  • psycopg2.extras.RealDictConnection

    • : OK
    • cursor.fetch*() : OK
  • psycopg2.extras.DictConnection

    • : KO - the returned row can't be accessed in a dict-fashion.
    • cursor.fetch*() : OK
>>> row =
>>> type(row)
<class 'psycopg2.extras.DictRow'>
>>> row['id']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/psycopg2/", line 155, in __getitem__
    x = self._index[x]
KeyError: 'id'
>>> row.keys()
  • psycopg2.extras.NamedTupleConnection
    • : KO - the returned row type is a tuple, not a named tuple.
    • cursor.fetch*() : OK
>>> row =
>>> type(row)
<type 'tuple'>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'id'
  • psycopg2._psycopg.connection
    • : OK
    • cursor.fetch() : OK

os-linux (Ubuntu 10.10)

Comments and changes to this ticket

  • Daniele Varrazzo

    Daniele Varrazzo January 10th, 2012 @ 02:10 AM

    • State changed from “new” to “invalid” is not part of the public interface, but an implementation detail: the cursor is iterable (you can call iter() on it), the fact it is also an iterator (exposing next()) is an implementation detail which may also change in the future.

    The artefacts you are seeing are due to __iter__ not being called before next(): the latter should only be called as part of the iteration protocol; the only well defined behaviour is that:

    cur = ...
    i = iter(cur)

    works as expected for any cursor type.

    The fact next() is not called __next__(), thus resembling a public method, has been acknowledged as a python design mistake: see

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