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 <kava...@gmail.com> 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 sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to