Bulk upsert using on_conflict_do_update

4,600 views
Skip to first unread message

Katie Wurman

unread,
Dec 2, 2016, 2:37:21 PM12/2/16
to sqlalchemy
What's the proper syntax for doing a bulk upsert using the new on_conflict_do_update clause? In postgres there is apparently a special table called 'excluded' which contains all the rows to be inserted, so the raw query looks like this: 

INSERT into MYTABLE(col1,col2,col3) VALUES (a,b,c),(d,e,f)
ON CONFLICT (col1) DO
UPDATE SET
col2 = excluded.col2
col3 = excluded.col3

Does sqlalchemy support bulk upserts like this yet? 

mike bayer

unread,
Dec 2, 2016, 3:41:19 PM12/2/16
to sqlal...@googlegroups.com
that is now supported using a special PG specific Insert object:


http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html?highlight=conflict#insert-on-conflict-upsert

examples of how to use "excluded" are present in that documentation section.



>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages