carambo carambo - 1 year ago 81
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.

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download