from sqlalchemy.dialects.postgresql import insert
class Foo(Base):
...
bar = Column(Integer)
foo = Foo(bar=1)
insert_stmt = insert(Foo).values(bar=foo.bar)
do_update_stmt = insert_stmt.on_conflict_do_update(
set_=dict(
bar=insert_stmt.excluded.bar,
)
)
session.execute(do_update_stmt)
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
Michael,Thanks for the quick response. In this example, `bar` is *not* the primary key, so I guess using the ORM here would be out of the question.What's your recommendation for refreshing the `foo` object after executing the `session.execute(do_update_stmt)` statement? Should I do an `INSERT...ON CONFLICT UPDATE...RETURNING id` and fetch the updated `foo` by ID, or is there a more preferred approach?
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/a765d0fa-329f-482c-a2d0-3e6ce0da5a8an%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/ef69252e-832f-43a9-aeba-03469f3f8971%40www.fastmail.com.