last_insert_id

PostgresWrapper.last_insert_id(table_name: str, pk_name='id')[source]

Get the last inserted ID for a given table + primary key.

Example:

>>> db = PostgresWrapper(db='my_db')
>>> db.action('INSERT INTO users (first_name, last_name) VALUES (?, ?);', ['John', 'Doe'])
>>> last_id = db.last_insert_id('users')
>>> db.fetchone('SELECT first_name, last_name FROM users WHERE id = ?', [last_id])
Record(id=16, first_name='John', last_name='Doe')
Parameters
  • table_name (str) – The table you want the last insertion for

  • pk_name (str) – The primary key name, e.g. id, username etc.

Return Any last_id

The last pk_name inserted into table_name