I am doing an INSERT into a postgres DB, which has an auto increment field called id.
How do I get the value of id after doing the insert in Python? There are good references on this site to this for MySQL databases (i.e. using cursor.lastrowid, or connection.insert_id()) but these don't seem to work with my postgres DB.
The id field is using a sequence to auto increment. So the id field is not null, default, nextval('table_id_seq')
Thanks
Pls try that:
cursor.execute("INSERT INTO .... RETURNING
id_of_new_row = cursor.fetchone()[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With