sqalchemy更新bindparam主键

时间:2019-11-02 05:54:03

标签: python sqlalchemy asyncpg

以下代码抛出“ sqlalchemy.exc.CompileError:未使用的列名:_id”。

db

Databasedatabases$('.type label').on('click', function(e){ e.preventDefault(); // Remove the 'checked' class from all of my siblings' input fields (including me): $(this).parent().siblings().each( function() { $(this).find('input').removeClass('checked'); } ); // Add the 'checked' class to my input field: $(this).parent().find('input').addClass('checked'); }) 的实例。注意,我必须使用名称“ _id”,因为SQLalchemy说“ id”是保留的。

除了更新每行的个性化之外,还有其他解决方案吗?

1 个答案:

答案 0 :(得分:1)

Database.execute_many()调用Connection.execute_many(),将您的查询分解为单独的单个查询(values中的每个元素一个),这是方法(source):

    async def execute_many(
        self, query: typing.Union[ClauseElement, str], values: list
    ) -> None:
        queries = [self._build_query(query, values_set) for values_set in values]
        async with self._query_lock:
            await self._connection.execute_many(queries)

请注意,它调用了_build_query()方法(source):

    @staticmethod
    def _build_query(
        query: typing.Union[ClauseElement, str], values: dict = None
    ) -> ClauseElement:
        if isinstance(query, str):
            query = text(query)

            return query.bindparams(**values) if values is not None else query
        elif values:
            return query.values(**values)

        return query

由于您没有传递str查询,而是传递值,因此控件进入elif values:条件处理,其中将各个值的dict解压缩到您的.values()方法中查询(Update.values())。从本质上讲,这使得查询正在尝试编译它:

query = (
    User.update()
    .where(User.c.id == bindparam("_id"))
    .values(score=bindparam("score"))
    .values(score=2, _id=1)
)

第二个值子句会导致使用新的绑定参数进行新的更新,这些绑定参数试图为score_id设置值。这将导致查询编译失败,因为表上没有_id列。

所以重现错误的MCVE确实是这样:

from sqlalchemy.dialects import postgresql

User.update().values(score=2, _id=1).compile(dialect=postgresql.dialect())

哪个加薪:

Traceback (most recent call last):
  File ".\main.py", line 31, in <module>
    User.update().values(score=2, _id=1).compile(dialect=postgresql.dialect())
  File "<string>", line 1, in <lambda>
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\elements.py", line 462, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\elements.py", line 468, in _compiler
    return dialect.statement_compiler(dialect, self, **kw)
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 571, in __init__
    Compiled.__init__(self, dialect, statement, **kwargs)
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 319, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 350, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\visitors.py", line 92, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 2569, in visit_update
    self, update_stmt, crud.ISUPDATE, **kw
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\crud.py", line 62, in _setup_crud_params
    return _get_crud_params(compiler, stmt, **kw)
  File "C:\Users\peter\Documents\git\stackoverflow\58668615-sqalchemy-update-bindparam-primary-key\.venv\lib\site-packages\sqlalchemy\sql\crud.py", line 177, in _get_crud_params
    % (", ".join("%s" % c for c in check))
sqlalchemy.exc.CompileError: Unconsumed column names: _id

要概括此问题,请使用将绑定参数同时传递到Update.where()Update.values()的查询来构建。然后,您将该查询和您的值传递到Database.execute_many(),在其中它们将您的值列表中的各个元素解压缩到您的查询中的第二次调用Update.values()中,从而用试图为其设置值的查询替换您的查询。 _id列不存在。

  

除了更新每行的个性化之外,还有其他解决方案吗?

使用sqlalchemy引擎和查询时,查询工作正常:

# using a sqlalchemy engine
engine.execute(query, values)

否则,应该以字符串形式将查询发送到Database.execute_many(),因为这意味着查询将在if isinstance(query, str):方法的_build_query()部分中处理,这将避免对查询进行的第二次.values()调用:

db.execute_many(str(query), values)