SQLAlchemy 1.4.17我正在尝试在 pytest 中进行简单的查询
def test_first():
engine = create_engine(settings.SQLALCHEMY_DATABASE_URI)
result = engine.execute(text("SELECT email FROM user"))
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误
Exception has occurred: MissingGreenlet
greenlet_spawn has not been called; can't call await_() here. Was IO attempted in an unexpected place? (Background on this error at: http://sqlalche.me/e/14/xd2s)
File "/Users/mattc/Development/inference/server/inference_server/app/tests/test_01_user.py", line 27, in test_first
result = engine.execute(text("SELECT email FROM user"))
Run Code Online (Sandbox Code Playgroud)
不知道为什么?有什么建议么?
sna*_*erb 16
您正在尝试以与同步连接器相同的方式使用异步连接器包
>>> import sqlalchemy as sa
>>> engine = sa.create_engine('postgresql+asyncpg:///')
>>> res = engine.execute(sa.text('SELECT 1'))
<stdin>:1: RemovedIn20Warning: The Engine.execute() method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by the Connection.execute() method of Connection, or in the ORM by the Session.execute() method of Session. (Background on SQLAlchemy 2.0 at: http://sqlalche.me/e/b8d9)
Traceback (most recent call last):
...
sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_() here. Was IO attempted in an unexpected place? (Background on this error at: http://sqlalche.me/e/14/xd2s)
Run Code Online (Sandbox Code Playgroud)
您需要使用同步连接器,例如 psycopg2、pg8000,或编写异步代码:
>>> import sqlalchemy as sa
>>> engine = sa.create_engine('postgresql+asyncpg:///')
>>> res = engine.execute(sa.text('SELECT 1'))
<stdin>:1: RemovedIn20Warning: The Engine.execute() method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by the Connection.execute() method of Connection, or in the ORM by the Session.execute() method of Session. (Background on SQLAlchemy 2.0 at: http://sqlalche.me/e/b8d9)
Traceback (most recent call last):
...
sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_() here. Was IO attempted in an unexpected place? (Background on this error at: http://sqlalche.me/e/14/xd2s)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21050 次 |
| 最近记录: |