Asken
Asken

Reputation: 8051

SQLAlchemy session after exception

In SQLAlchemy, after an exception, the session needs to be set again because of the rollback. Should I therefor always set the session between calls?

user = User('username', 'Full Name', 'password', 'email',
                datetime.now(), 'username')
session.add(user)
try:
    session.commit()
except SQLAlchemyError:
    pprint('Not quite right...')

# the session needs to be re-instantiated in case of an exception.
# should I always do it or only if there was an exception above?
session = Session()
res = session.query(User).all()
...

Upvotes: 2

Views: 1472

Answers (1)

JosefAssad
JosefAssad

Reputation: 4128

Fish:

try:
    session.commit()
except SQLAlchemyError:
    pprint('Not quite right...')
    session.rollback()

Fishing rod.

Upvotes: 3

Related Questions