Skip to content

Commit

Permalink
Filters are not optional in add_and_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Nov 19, 2015
1 parent fb8b0bb commit 738aa1e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions coaster/db.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from sqlalchemy import exc
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound
from flask.ext.sqlalchemy import SQLAlchemy as SQLAlchemyBase, SignallingSession

__all__ = ['SQLAlchemy', 'db']
Expand All @@ -26,10 +27,12 @@ def add_and_commit(self, _instance, **filters):
self.add(_instance)
self.commit()
return _instance
except exc.IntegrityError:
except IntegrityError as e:
self.rollback()
if filters:
try:
return self.query(_instance.__class__).filter_by(**filters).one()
except NoResultFound: # Do not trap the other exception, MultipleResultsFound
raise e


# Provide a Flask-SQLAlchemy alternative that uses our custom session
Expand Down

0 comments on commit 738aa1e

Please sign in to comment.