On Nov 16, 2012, at 7:42 AM, sajuptpm wrote:
>
>
> The code bellow throwing error "ObjectDeletedError: Instance 'xxx' has been
> deleted." when a exception throwing from "method1".
> Eroor in line method1(db_obj1.id), db_obj1.id failing.
>
> How fix this issue.
>
>
> def main_method():
> DBSession.add(db_obj1)
> DBSession.fush()
>
> for x in lst:
> try:
> method1(db_obj1.id)
> excpt Exception, ex:
> pass
>
>
> def method1(id):
> try:
> s1 = DBSession()
> s1.begin_nested()
>
> db_obj2 = create_new_obj(id)
> DBSession.add(db_obj1)
> DBSession.fush()
>
> if some-codition:
> raise Exception("Failedd")
>
> s1.commit()
> except Exception, ex:
> s1.rollback()
> raise ex
this code is not written in a way that makes sense, as "db_obj2" is not
referenced and it's not clear where "db_obj1" comes from inside of method1().
The use of DBSession.<method>() vs. s1.<method>() also suggests some details
are being omitted here.
So without being able to tell what you're actually doing, the error means that
you are attempting to refer to an attribute on an object (like
my_object.some_attribute) after a transaction has been rolled back or
committed, and the row to which my_object refers to no longer exists. This row
might not exist because the transaction in which it was inserted was rolled
back, or the row was deleted by a different transaction.
After a transaction ends, accessing an attribute on your object will re-emit a
SELECT to the database in a new transaction.
Turning on echo=True or echo='debug' on your create_engine() can be a valuable
tool in debugging exactly what is happening in your code, including the
sequence of SELECT statements.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.