Here are the two options we have for invoking a statement:
try:
cursor.execute(stmt, params) # single row
except IntegrityError:
# …
try:
cursor.executemany(stmt, [params, params, params, params, …]) # many rows
except IntegrityError:
# …
There is no way to use the second form while being able to record the moment each parameter set is used, unless the DBAPI itself provides additional hooks for logging at this level. However, this logging would defeat some of the purpose of executemany(), which is that of processing many parameter sets at maximum speed.
The SQLAlchemy Session tries to use executemany() as often as it can within a flush() procedure; it can be used any time there are more than one row to be INSERTED where we already have the primary key value available.
If you’d like to operate on individual rows, I guess I wasn’t specific enough from my instruction to use SAVEPOINT, you should flush individually:
for obj in all_my_objects:
session.add(obj)
try:
with session.begin_nested():
session.flush()
except IntegrityError:
# deal with this error, don’t add obj, or however it is you intend to deal with existing rows
>
> Regards,
> Markus
>
> --
> 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.
> To post to this group, send email to
sqlal...@googlegroups.com.
> Visit this group at
http://groups.google.com/group/sqlalchemy.
> For more options, visit
https://groups.google.com/d/optout.