diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-12-11 14:30:18 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-12-11 14:30:18 -0500 |
| commit | 9087157749a0527d6af37e58166793fc7e2f0bf7 (patch) | |
| tree | 9e8c201bb5c01c84d14dd6ce2038955cd8d548d6 /lib/sqlalchemy/orm/session.py | |
| parent | 164bff07496c345c3c57a8b26439aa6a0fbce3b8 (diff) | |
| download | sqlalchemy-9087157749a0527d6af37e58166793fc7e2f0bf7.tar.gz | |
- The :class:`.exc.StatementError` or DBAPI-related subclass
now can accomodate additional information about the "reason" for
the exception; the :class:`.Session` now adds some detail to it
when the exception occurs within an autoflush. This approach
is taken as opposed to combining :class:`.FlushError` with
a Python 3 style "chained exception" approach so as to maintain
compatibility both with Py2K code as well as code that already
catches ``IntegrityError`` or similar.
Diffstat (limited to 'lib/sqlalchemy/orm/session.py')
| -rw-r--r-- | lib/sqlalchemy/orm/session.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 553f6de97..b3720c94e 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -1180,7 +1180,18 @@ class Session(_SessionClassMethods): def _autoflush(self): if self.autoflush and not self._flushing: - self.flush() + try: + self.flush() + except sa_exc.StatementError as e: + # note we are reraising StatementError as opposed to + # raising FlushError with "chaining" to remain compatible + # with code that catches StatementError, IntegrityError, + # etc. + e.add_detail( + "raised as a result of Query-invoked autoflush; " + "consider using a session.no_autoflush block if this " + "flush is occuring prematurely") + util.raise_from_cause(e) def refresh(self, instance, attribute_names=None, lockmode=None): """Expire and refresh the attributes on the given instance. |
