调用expunge_all后,实例未绑定到Session

时间:2017-05-04 07:11:27

标签: python python-3.x sqlalchemy

我有一个包含设备信息的表(有一个id属性是主键,由DB自动分配),我运行以下代码:

    session.autoflush = False
    try:
        devices = model.Device.get_all_by_name(session, dev_data.index.tolist())
        for idx, device in enumerate(devices):
            device.update(dev_data.ix[device.name])
            if 0 == idx % 500 and idx != 0:
                session.flush()
    except Exception as exp:
        # exception handling here
    else:
        session.commit()
        session.expunge_all()
        # calling the next line throws 'Instance not bound to a session' error
        logger.debug('Device info after expunge: {}'.format(devices[-1].name))
    finally:
        session.close()

每当我尝试访问其中一个设备的属性时,会抛出一个exeption,但是我想expunge_all()应该分离对象,并且在提交或刷新时,所有python对象都应该是snychronized,对吗?

0 个答案:

没有答案