This project is archived and is in readonly mode.

#154 ✓invalid
Psycopg website

Insert with RETURNING mutilple values returns them in a string

Reported by Psycopg website | March 24th, 2013 @ 07:32 PM

Submitted by: ChMeessen

When performing an SQL insert operation returning multiple values, which is valid, the value returned by fetchone()[0] is a string with values concatenated.

cur.execute("""INSERT INTO items
(userId, count, label, editId) VALUES (%s, %s, %s, %s)
RETURNING (id,editId,count,label)""",
(userId, 0, label, editId)) res = cur.fetchone()[0]

res is the string '(63,8,0,"Test")'
instead of a tuple of values.

When RETURNING id is used, the returned value is the id itself.

Comments and changes to this ticket

  • Daniele Varrazzo

    Daniele Varrazzo March 25th, 2013 @ 09:38 AM

    • State changed from “new” to “invalid”

    That's because you have put the RETURNING values into parenthesis, so postgres is returning a record value for which psycopg doesn't have a typecaster.

    You have to use:

    cur.execute("""INSERT INTO items
        (userId, count, label, editId) VALUES (%s, %s, %s, %s)
        RETURNING id,editId,count,label""",

    This is no psycopg bug, so closing. bye!

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