does the executemany() limitation with result sets apply to INSERT..RETURNING ?

Michael Bayer <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
I notice the 2.0.9 changelog mentions this:

- executemany() now return the numer of total INSERTed or UPDATEd
    rows. Note that, as it has always been, executemany() should not
    be used to execute multiple SELECT statements and while it will
    execute the statements without any problem, it will return the
    wrong value.

This appears to be the case as well for INSERT...RETURNING as in the  
example below.

I guess there's nothing to be done here unless its a surprise to the  
developers (well, if psycopg2 ever gets a wiki / bugtracker / semi- 
usable website at some point, that might be nice).


import psycopg2
c = psycopg2.connect(user='scott', password='tiger', host='localhost',  
database='test')

cursor = c.cursor()

cursor.execute("""
CREATE TABLE tables (
	id SERIAL NOT NULL,
	persons INTEGER,
	"full" BOOLEAN,
	PRIMARY KEY (id)
)
""")

cursor.execute('INSERT INTO tables (persons, "full") VALUES (% 
(persons)s, %(full)s) RETURNING tables.id, tables.persons,  
tables."full"',
     {'persons': 1, 'full': False}
)

# passes
ret = cursor.fetchall()
assert ret == [(1, 1, False)], ret

cursor.executemany('INSERT INTO tables (persons, "full") VALUES (% 
(persons)s, %(full)s) RETURNING tables.id, tables.persons,  
tables."full"',
     [{'persons': 2, 'full': False}, {'persons': 3, 'full': True}]
)

# fails, returns [(3, 3, True), (None, None, None)]
ret = cursor.fetchall()
assert ret == [(2, 2, False), (3, 3, True)], ret
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.