SQLAlchemy insert on conflict (upsert) multiple rows at once

Viewed 30

I know there are commands in SQLAlchemy called bulk_insert_mappings and bulk_update_mappings, which can insert/update multiple rows at once; and I know there is a way to upsert a single row like so:

from sqlalchemy.dialects.postgresql import insert
insert_stmt = insert(my_table).values(
     id='some_existing_id',
     data='inserted value'
     )
do_update_stmt = insert_stmt.on_conflict_do_update(
        index_elements=['id'],
        set_=dict(data='updated value')
     )
conn.execute(do_update_stmt)

What I would like to do is combine these two things into a bulk upsert without having to ping my database three times (once to see which rows I need to insert and which to update, once to do the actual insert and once for an update). Does anyone know if there is a way to do that? Thanks for any help in advance!

0 Answers
Related