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.

Insert with RETURNING mutilple values returns them in a string

#154

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.

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

State: invalid
Milestone: none
Assigned to: nobody

Activity

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

    March 25th, 2013 @ 09:38 AM