You're not supposed to be able to get results from executemany
:
The function is mostly useful for commands that update the database: any result set returned by the query is discarded.
Per the psycopg2 docs.
You'll be better off looping over a single insert
within a transaction, or using a multi-valued insert... returning
, though in the latter case you must be careful to match returned IDs using another input value, you can't just assume the order of returned IDs is the same as the input VALUES
list.
When I run your test locally, it simply fails:
>>> import psycopg2
>>> conn = psycopg2.connect("dbname=regress")
>>> curs = conn.cursor()
>>> curs.execute("create table my_table(id serial primary key, field_1 integer, field_2 integer);")
>>> data = [(0, 0), (0, 0)]
>>> curs.executemany(
... "INSERT INTO my_table (field_1, field_2) "
... "VALUES (%s, %s) RETURNING id;",
... data
... )
>>>
>>> curs.fetchall()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
psycopg2.ProgrammingError: no results to fetch
Tested with psycopg2 2.5.1.