catching SQLAlchemy exceptions

前端 未结 3 591
长发绾君心
长发绾君心 2021-01-31 14:21

What is the upper level exception that I can catch SQLAlechmy exceptions with ?

>>> from sqlalchemy import exc
>>> dir(exc)
[\'ArgumentError\',         


        
3条回答
  •  死守一世寂寞
    2021-01-31 14:30

    To catch any exception SQLAlchemy throws:

    from sqlalchemy import exc
    db.add(user)
    try:
      db.commit()
    except exc.SQLAlchemyError:
      pass # do something intelligent here
    

    See help(sqlalchemy.exc) and help(sqlalchemy.orm.exc) for a list of possible exceptions that sqlalchemy can raise.

提交回复
热议问题