Psycopg2 returns none when execute many is combined with returning id

流过昼夜 提交于 2019-12-13 03:31:58

问题


I want to insert many values into a table using execute_values. I also need the ids of the affected rows. I want to use RETURNING id. I have set the fetch flag. Still, the result is None.

  • postgres 11
  • python 3.6.9
  • psycopg2 '2.8.3 (dt dec pq3 ext lo64)'

I create a table with

CREATE TABLE dummy_table (
    id serial,
    name text,
    age int
);

The values and query are

values = [('bob', 10), ('alice', 12)]
query = 'INSERT INTO dummy_table (name, age) VALUES %s RETURNING id;'

I call execute values like this

result = psycopg2.extras.execute_values(cursor, query, values, fetch=True)
connection.commit()
print(result)
> None

What am I doing wrong?

来源:https://stackoverflow.com/questions/59085374/psycopg2-returns-none-when-execute-many-is-combined-with-returning-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!