On 05/29/2017 11:34 PM, Михаил Доронин wrote:
Umm, what I've meant is how to use postgresql
on_conflict_do_update in such a way that sqlalchemy would use executemany
behind the scenes.
In examples it usage looks like this.
stmt = insert(table, values)
stmt = stmt.on_conflict_do_update(set_=dict(a=stmt.excluded.a))
excluded is generated from values, right? If I don't pass values to the
statement, how can I use excluded?
you would not use insert(table, values), which as we reviewed earlier is
not "executemany" syntax. the values are passed to execute() as the
second argument, and is the list of values which are invoked for the
statement one at a time. .excluded is a server side collection
generated by Postgresql and is based on the current row being operated
upon. These values are not returned to the client. They are only used
in context of the statement.
think of executemany like this:
stmt =
table.insert().on_conflict_do_update(set_=dict(a=stmt.excluded.a)).values(x
= bindparam('x'), y=bindparam('y'))
def executemany(stmt, values):
for value in values:
conn.execute(stmt, value)
executemany(stmt, [{"x": 1, "y": 2}, {"x": 3, "y": 4}, ...])
e.g. if your statement works for one execute() and one set of
parameters, it will work for any number of individual sets of parameters.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.