carambo carambo - 8 months ago 24
Python Question

What's the equivalent of peewee's DoesNotExist in SQLAlchemy?

I've been using peewee with SQLite for some time and now I'm switching to SQLAlchemy with Postgres and I can't find equivalent of DoesNotExist (see example)

return models.User.get( == userid)
except models.DoesNotExist:
return None

Do you know how to achieve the same with SQLAlchemy? I've checked stuff which I can import from sqlalchemy.ext but nothing seemed right.


The closest could be this one: -

Code Sample:

from sqlalchemy.orm.exc import NoResultFound

    user = session.query(User).one()
except NoResultFound, e:
    print "No users found"