Skip to content

Instantly share code, notes, and snippets.

@tomsing1
Forked from amorgun/sample.py
Created August 9, 2018 22:14
Show Gist options
  • Save tomsing1/b2202eab5a0ff040b940cef6aaeb09d7 to your computer and use it in GitHub Desktop.
Save tomsing1/b2202eab5a0ff040b940cef6aaeb09d7 to your computer and use it in GitHub Desktop.
SqlAlchemy postgres bulk upsert
from sqlalchemy.dialects import postgresql
def bulk_upsert(session: Session,
items: Sequence[Mapping[str, Any]]):
session.execute(
postgresql.insert(MyModel.__table__)
.values(items)
.on_conflict_do_update(
index_elements=[MyModel.id],
set_={MyModel.my_field.name: 'new_value'},
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment