flask SQLAlchemy 查询也要回滚吗?拿不到数据

noel
  • 100

先上代码:

driver = db.session.query(Driver,Avatars).outerjoin(Avatars).filter_by(telephone=telephone).all()
LOG.info(driver) # INFO in views: [(Driver name: websocket, age: 25 telephone:156***4289, Driver's avatar is telephone:156****4289, avatar:/home/nvidia/Public/avicii-02.png, type:0)]
#LOG.debug(driver.__slots__) # 'list' object has no attribute '__slots__'
if(driver):
    driverList = []
    for dr in driver:
        LOG.debug(dr.__slots__) #输出  [2019-07-03 14:32:43,402] DEBUG in views: ()
        driverList.append(dr.__slots__)
    return jsonify({"status":0,"msg":driverList})

说明

LOG.info(driver)控制台输出了查询结果,但是reture的driverList是[],postman显示内容如下:

{
    "msg": [
        []
    ],
    "status": 0
}

控制台日志如下:

2019-07-03 10:24:29,480 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2019-07-03 10:24:29,481 INFO sqlalchemy.engine.base.Engine SELECT driver.id AS driver_id, driver.name AS driver_name, driver.telephone AS driver_telephone, driver.age AS driver_age, avatars.id AS avatars_id, avatars.type AS avatars_type, avatars.telephone AS avatars_telephone, avatars.avatar AS avatars_avatar, avatars.`character` AS avatars_character
FROM driver LEFT OUTER JOIN avatars ON driver.telephone = avatars.telephone
WHERE avatars.telephone = %(telephone_1)s
2019-07-03 10:24:29,481 INFO sqlalchemy.engine.base.Engine {'telephone_1': '156****4289'}
#下面一条日志是LOG.info()显示的内容
[2019-07-03 10:24:29,485] INFO in views: [(Driver name: websocket, age: 25 telephone:156****4289, Driver's avatar is telephone:156****4289, avatar:/home/nvidia/Public/avicii-02.png, type:0)]  
[2019-07-03 14:32:43,402] DEBUG in views: ()
2019-07-03 10:24:29,485 INFO sqlalchemy.engine.base.Engine ROLLBACK
127.0.0.1 - - [03/Jul/2019 10:24:29] "GET /driver/detail/156****4289 HTTP/1.1" 200 -

倒数第二条日志,返回数据给请求的时候,为什么回滚了呢?是我代码少写了什么吗?

回复
阅读 2.3k
2 个回答
✓ 已被采纳

__slots__是获取class的限制属性,并不是获取类的值,因为你的Driver类并没有定义__slots__,所以取出来是空值

宣传栏