我在用async_engine。当我试图执行任何事情时:
async with self.async_engine.connect() as con:
query = "SELECT id, name FROM item LIMIT 50;"
result = await con.execute(f"{query}")
我得到了:
Exception has occurred: ObjectNotExecutableError
Not an executable object: 'SELECT id, name FROM item LIMIT 50;'
这个问题以前是由用户@二苯乙烯提出的,但现在是从SO中删除了。
我是在谷歌搜索缓存中找到的,这是副本。
我也有同样的问题,所以我重新考虑了一下,但最初的版本如下:
我试图从元数据创建表,如下所示:
Base = declarative_base()
properties = Table(
'properties', Base.metadata,
# ...
Column('geolocation', Geography(geometry_type='POINT', srid=4326)),
# ...
)
engine = create_async_engine("postgresql+asyncpg://user:password@postgres/")
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
给出以下错误:
sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: 'CREATE INDEX "idx_properties_geolocation" ON "properties" USING GIST ("geolocation")'
考虑到这个文档
版本:
发布于 2021-10-08 05:44:20
正如异常消息所示,str
'SELECT id, name FROM item LIMIT 50;'
不是可执行对象。若要使其可执行,请使用sqlalchemy.text包装它。
from sqlalchemy import text
async with self.async_engine.connect() as con:
query = "SELECT id, name FROM item LIMIT 50;"
result = await con.execute(text(query))
async.connection.execute要求它的语句参数
..。始终是ClauseElement和可执行层次结构中的对象,包括:
同步connection.execute方法允许原始字符串,但在SQLAlchemy 2.0中这是不推荐的,并标记为删除。
https://stackoverflow.com/questions/69490450
复制相似问题