On Jul 23, 2014, at 4:52 PM, Milind Vaidya <[email protected]> wrote:
> Well only caveat here is that I am inserting a list of objects in one go > using session.add_all() if you do an add_all(), then say flush(), it will emit INSERT statements for all of them. If any INSERT fails, the whole operation is rolled back. there is a tradeoff here between atomicity and ability to skip failures (as opposed to preventing them ahead of time). you have to make that choice. > > On Wednesday, July 23, 2014 2:01:18 PM UTC-5, Michael Bayer wrote: > use a savepoint: > > from sqlalchemy import exc > try: > with session.begin_nested(): > session.add(MyObject()) > session.flush() > except exc.IntegrityError: > pass > session.commit() > > > personally I prefer just to emit a SELECT first, which may be a per-row > SELECT or may be a prefetch of all the rows from the table in question. > > > > On Jul 23, 2014, at 2:15 PM, Milind Vaidya <[email protected]> wrote: > >> I have a script which updates DB periodically. There are some base table >> which may undergo some change but very rarely. In such case if this script >> tries to insert the already present value it will throw integrity error. >> Is there any way of dealing with this other that first fetch the value >> manually and then only insert if not present ? >> >> -- >> 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 http://groups.google.com/group/sqlalchemy. >> For more options, visit https://groups.google.com/d/optout. > > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
