Python烧瓶删除500内部服务器错误

时间:2016-03-07 04:50:13

标签: python flask sqlalchemy

我在 DELETE def:

中遇到500内部服务器错误
def delete(self, flight_id):
    session = db.get_session()
    try:
        spend_for_flight = session.query(func.count(db.Snapshot.rowID))\
            .join(db.Flight, db.Flight.strategy_id == db.Snapshot.strategy_id)\
            .filter(and_(db.Snapshot.interval >= db.Flight.start_date, db.Snapshot.interval <= db.Flight.end_date, db.Flight.rowID == flight_id)).scalar();

        # there is spend for at least one of these flight dates
        if spend_for_flight > 0:
            response = jsonify(error="Flight has spend. Cannot delete.")
            response.status = 400
            return response
        elif spend_for_flight == 0:
            session.query(db.Flight).filter(db.Flight.rowID == flight_id).delete()
            session.commit()
            return 204
    except sqlalchemy.exc.SQLAlchemyError, exc:
        session.rollback()
        reason = exc.message
        response = jsonify(error=reason)
        response.status_code = 501
        return response
    finally:
        session.close()

错误发生在if语句中。 SQL alchemy查询运行正常并且spend_for_flight检出(其他成功)并且航班成功删除。 spend_for_flight >0时,我收到服务器错误。

由于

1 个答案:

答案 0 :(得分:1)

您应该使用status_code代替status,就像使用501一样。也就是说,而不是:

response.status = 400

response.status_code = 400