• <legend id='xY8gn'><style id='xY8gn'><dir id='xY8gn'><q id='xY8gn'></q></dir></style></legend>
      <bdo id='xY8gn'></bdo><ul id='xY8gn'></ul>
    <tfoot id='xY8gn'></tfoot>

      <i id='xY8gn'><tr id='xY8gn'><dt id='xY8gn'><q id='xY8gn'><span id='xY8gn'><b id='xY8gn'><form id='xY8gn'><ins id='xY8gn'></ins><ul id='xY8gn'></ul><sub id='xY8gn'></sub></form><legend id='xY8gn'></legend><bdo id='xY8gn'><pre id='xY8gn'><center id='xY8gn'></center></pre></bdo></b><th id='xY8gn'></th></span></q></dt></tr></i><div id='xY8gn'><tfoot id='xY8gn'></tfoot><dl id='xY8gn'><fieldset id='xY8gn'></fieldset></dl></div>

      <small id='xY8gn'></small><noframes id='xY8gn'>

        如何在 sqlalchemy 中使用子查询添加行?

        How to Add rows using subqueries in sqlalchemy?(如何在 sqlalchemy 中使用子查询添加行?)
          <bdo id='u5wr9'></bdo><ul id='u5wr9'></ul>

          1. <i id='u5wr9'><tr id='u5wr9'><dt id='u5wr9'><q id='u5wr9'><span id='u5wr9'><b id='u5wr9'><form id='u5wr9'><ins id='u5wr9'></ins><ul id='u5wr9'></ul><sub id='u5wr9'></sub></form><legend id='u5wr9'></legend><bdo id='u5wr9'><pre id='u5wr9'><center id='u5wr9'></center></pre></bdo></b><th id='u5wr9'></th></span></q></dt></tr></i><div id='u5wr9'><tfoot id='u5wr9'></tfoot><dl id='u5wr9'><fieldset id='u5wr9'></fieldset></dl></div>
                  <tbody id='u5wr9'></tbody>

                  <legend id='u5wr9'><style id='u5wr9'><dir id='u5wr9'><q id='u5wr9'></q></dir></style></legend>

                  <small id='u5wr9'></small><noframes id='u5wr9'>

                  <tfoot id='u5wr9'></tfoot>
                  本文介绍了如何在 sqlalchemy 中使用子查询添加行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用带有 SQLAlchemy 的 Postgresql,但似乎 sqlalchemy 在使用子查询时无法添加行.

                  I'm using Postgresql with SQLAlchemy but it seems sqlalchemy is having trouble adding rows when using subqueries.

                  在我的示例中,我想为表中的特定标签更新计数器.

                  In my example, I want to update a counter for a specific tag in a table.

                  在 SqlAlchemy 中,测试运行类如下所示:

                  In SqlAlchemy a test run class would look like the following:

                  class TestRun( base ):
                      __tablename__   = 'test_runs'
                  
                      id              = sqlalchemy.Column( 'id', sqlalchemy.Integer, sqlalchemy.Sequence('user_id_seq'), primary_key=True )
                      tag             = sqlalchemy.Column( 'tag', sqlalchemy.String )
                      counter         = sqlalchemy.Column( 'counter', sqlalchemy.Integer )
                  

                  插入代码应如下所示:

                  tag = 'sampletag'
                  counterquery = session.query(sqlalchemy.func.coalesce(sqlalchemy.func.max(TestRun.counter),0) + 1).
                                              filter(TestRun.tag == tag).
                                              subquery()
                  
                  testrun = TestRun()
                  testrun.tag = tag
                  testrun.counter = counterquery
                  
                  session.add( testrun )
                  session.commit()
                  

                  这个问题是,它在运行这段代码时给出了一个非常有趣的错误,它正在尝试运行以下 SQL 查询:

                  The problem with this, is it gives a very interesting error when running this code, it's trying to run the following SQL Query:

                  'INSERT INTO test_runs (id, tag, counter) 
                      VALUES (%(id)s, 
                              %(tag)s, 
                              SELECT coalesce(max(test_runs.counter), %(param_1)s) + %(coalesce_1)s AS anon_1 
                                 FROM test_runs 
                                 WHERE test_runs.tag = %(tag_1)s)' 
                   {'coalesce_1': 1, 'param_1': 0, 'tag_1': 'mytag', 'tag': 'mytag', 'id': 267L}
                  

                  这看起来很合理,只是在 SELECT 调用周围缺少括号.当我手动运行 SQL 查询时,它给了我与 sqlalchemy 给我的完全相同的错误,直到我手动输入括号来修复所有问题.似乎是一个不太可能的错误,sqlalchemy 在需要时会忘记放括号,所以我的问题是我是否缺少在使用 sqlalchemy 添加行时正确使用子查询的函数?

                  Which looks reasonable, except it's missing parenthesis around the SELECT call. When I run the SQL query manually it gives me the same exact error that sqlalchemy gives me until I type in the parenthesis manually which fixes everything up. Seems like an unlikely bug that sqlalchemy would forget to put parenthesis when it needs to, so my question is am I missing a function to use subqueries correctly when adding rows using sqlalchemy?

                  推荐答案

                  而不是使用 subquery() 调用 as_scalar() 方法:

                  Instead of using subquery() call as_scalar() method:

                  返回此 Query 表示的完整 SELECT 语句,转换后到一个标量子查询.

                  Return the full SELECT statement represented by this Query, converted to a scalar subquery.

                  示例:

                  具有分类父子关系的模型:

                  Models with classing parent-child relationship:

                  class Parent(Base):
                      __tablename__ = 'parents'
                      id = Column(Integer, primary_key=True)
                      counter = Column(Integer, nullable=False, default=0)
                  
                  class Child(Base):
                      __tablename__ = 'children'
                      id = Column(Integer, primary_key=True)
                      parent_id = Column(ForeignKey(Parent.id), nullable=False)
                      parent = relationship(Parent)
                  

                  更新counter字段的代码:

                  parent.counter = session.query(func.count(Child.id))
                                      .filter_by(parent=parent).as_scalar()
                  

                  产生的 SQL(从日志中复制):

                  Produced SQL (copied from the log):

                  UPDATE parents SET counter=(SELECT count(children.id) AS count_1 
                  FROM children
                  WHERE ? = children.parent_id) WHERE parents.id = ?
                  

                  这篇关于如何在 sqlalchemy 中使用子查询添加行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                    • <tfoot id='5ipJA'></tfoot>

                        <small id='5ipJA'></small><noframes id='5ipJA'>

                        <i id='5ipJA'><tr id='5ipJA'><dt id='5ipJA'><q id='5ipJA'><span id='5ipJA'><b id='5ipJA'><form id='5ipJA'><ins id='5ipJA'></ins><ul id='5ipJA'></ul><sub id='5ipJA'></sub></form><legend id='5ipJA'></legend><bdo id='5ipJA'><pre id='5ipJA'><center id='5ipJA'></center></pre></bdo></b><th id='5ipJA'></th></span></q></dt></tr></i><div id='5ipJA'><tfoot id='5ipJA'></tfoot><dl id='5ipJA'><fieldset id='5ipJA'></fieldset></dl></div>

                          <tbody id='5ipJA'></tbody>
                        <legend id='5ipJA'><style id='5ipJA'><dir id='5ipJA'><q id='5ipJA'></q></dir></style></legend>

                          • <bdo id='5ipJA'></bdo><ul id='5ipJA'></ul>