2

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)

try:
    return models.User.get(models.User.id == 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.

carambo
  • 57
  • 9
  • 1
    Might be helpful: https://stackoverflow.com/questions/18110033/getting-first-row-from-sqlalchemy – Blender Aug 14 '16 at 00:39

2 Answers2

5

The closest could be this one: - http://docs.sqlalchemy.org/en/latest/orm/exceptions.html#sqlalchemy.orm.exc.NoResultFound

Code Sample:

from sqlalchemy.orm.exc import NoResultFound

try:
    user = session.query(User).one()
except NoResultFound, e:
    print "No users found"
masnun
  • 11,635
  • 4
  • 39
  • 50
-2

Peewee does work with Postgresql, you know. ;)

coleifer
  • 24,887
  • 6
  • 60
  • 75