已弃用的 Session.close_all() 与 'scoped_session' 对象没有属性 'close_all_sessions'

问题描述 投票:0回答:2

我尝试在服务测试失败或在 Flask 应用层完成后使用“drop_all”:

@pytest.fixture(scope='class')
def db_connection():
    db_url = TestConfig.db_url
    db = SQLAlchemyORM(db_url)
    db.create_all(True)
    yield db_connection
    db.drop_all()

当某些测试通过时,“drop_all”会起作用,但当它失败时,测试就会冻结。

所以,这个解决方案解决了我的问题: https://stackoverflow.com/a/44437760/3050042

不幸的是,我搞砸了。

当我使用“Session.close_all()”时,SQLAlchemy 警告:

The Session.close_all() method is deprecated and will be removed in a future release.  Please refer to session.close_all_sessions().

当我更改建议时:

AttributeError: 'scoped_session' object has no attribute 'close_all_sessions'

是的,我使用scoped_session和纯SQLAlchemy。

如何解决这个问题?

flask sqlalchemy
2个回答
10
投票

close_all_sessions
函数定义在
sqlalchemy.orm.session
的顶层。在撰写此答案时,here 就是它的样子。因此,您可以按如下方式使用它。

from sqlalchemy.orm.session import close_all_sessions


close_all_sessions()

0
投票

asyncio(AsyncSession)模拟:

from sqlalchemy.ext.asyncio import close_all_sessions

await close_all_sessions()
© www.soinside.com 2019 - 2024. All rights reserved.