-
-
Save tomsing1/b2202eab5a0ff040b940cef6aaeb09d7 to your computer and use it in GitHub Desktop.
SqlAlchemy postgres bulk upsert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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