On May 31, 2011, at 11:09 PM, chris e wrote: > I have a field verification routine that is run as part of a mapper > extension. When a field error is detected, an exception is thrown with > the field in question, the object with the incorrect field. This > worked great in 0.3, but I'm now moving to 0.6, and I can no longer do > this as my invalid filed exception is using out the __repr__ value of > the object with the incorrect field, and the __repr__ is causing > sqlalchemy to attempt to query the db to retrieve attribute values > because the rollback caused the object to be expired. > > Is there a way to retrieve the attribute values without having > sqlalchemy run a query? I tried object.attribute.get_history, but I > end up with an empty history item.
If you want 0.3's behavior you can set _enable_transaction_accounting=False on your Session(), that will disable the expiration on rollback()... but then you lose a big part of how the Session maintains state against the transaction. Otherwise, you can call rollback() on your Session after the error and fully close out the previous transaction. Then the __repr__() will re-fetch the data in the new transaction. The data that's present in __dict__ after the transaction has been rolled back otherwise may be stale, which is why it's expired. We looked into trying to rollback in memory only some years ago but it proved to be tremendously complicated for little gain, compared to the expiration which is completely simple and foolproof. Exceptions within a flush() shouldn't be considered to be a normal occurrence in any case - the application should try to avoid using exceptions as a "lazy" means of checking state in the DB. Databases like Postgresql don't allow work to continue in a transaction after certain classes of errors have occurred in any case. -- 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.
