On Feb 15, 2012, at 12:21 AM, Manav Goel wrote:
> My use case requires that if insertion of object of Myclass succeeds
> then insert Object of Myclass1 .
> Even if inserting of Myclass1 object fails insertion of Myclass should
> not be rolled back.
> I mean adding Myclass is permanent and does not depend on failure or
> success of insertion of Myclass1.
> I have written following code and want to know if am understanding
> right usage of begin_nested and not writing buggy code.
> I am using postgresql 9.0
> try:
> obj =Myclass()
> session.add(obj)
> if condition true:
>session.begin_nested()
>try:
>n = Myclass1(arguments)
>session.add(n)
>except SQLAlchemyError:
>db_session.rollback()
>
> session.commit()
> except SQLAlchemyError:
> session.rollback()
> raise
>
> Code is running f9, just want to make sure of some unknown gotcha in
> this code.
> Other option will be I commit after adding Myclass and perform
> insertion of Myclass1 in separate transaction but this way is not
> appealing to me.
assuming that "db_session" is a typo for "session", then yes this is the basic
idea.There's a slight gotcha in that you're not inspecting the actual
SQLAlchemyError coming in, you might want to limit that to IntegrityError which
is what psycopg2 usually throws for these.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.