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

      <tfoot id='weIf7'></tfoot>
    1. <small id='weIf7'></small><noframes id='weIf7'>

      1. sqlalchemy 与 `len(query.all())` 和 `query.count()` 的值不同

        sqlalchemy different value from `len(query.all())` and `query.count()`(sqlalchemy 与 `len(query.all())` 和 `query.count()` 的值不同)

        • <small id='vq9xb'></small><noframes id='vq9xb'>

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

                  <tfoot id='vq9xb'></tfoot>

                  本文介绍了sqlalchemy 与 `len(query.all())` 和 `query.count()` 的值不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是一个示例代码.

                  一个文档有很多评论

                  PostComment 扩展了 Comment(具有 sqlalchemy 多态特性)

                  PostComment extends Comment (with sqlalchemy polymorphic feature)

                  某些查询在 len(query.all())query.count()

                  • sqlalchemy 版本:1.0.8
                  • mysql版本:5.6.25

                  见下面的主要功能.

                  发生了什么?

                  from sqlalchemy import create_engine
                  from sqlalchemy.orm import sessionmaker, scoped_session
                  from sqlalchemy.ext.declarative import declarative_base
                  from sqlalchemy import Table, Column, Integer, Float, Boolean, ForeignKey, String, Unicode, DateTime, Date, UniqueConstraint
                  from sqlalchemy.orm import relationship, backref
                  
                  engine = create_engine('mysql://root:root@192.168.59.103:3306/document')
                  
                  DBSession = scoped_session(sessionmaker(bind=engine))
                  
                  Base = declarative_base()
                  Base.metadata.bind = engine
                  
                  class Document(Base):
                      __tablename__ = 'document'
                  
                      id = Column(Integer, primary_key=True)
                  
                  
                  class Comment(Base):
                      __tablename__ = 'comment'
                  
                      id = Column(Integer, primary_key=True)
                      type = Column(String(50))
                      document_id = Column(Integer, ForeignKey('document.id'), primary_key=True)
                      document = relationship('Document', backref=backref('comments', lazy='dynamic'))
                  
                      __mapper_args__= {
                          'polymorphic_identity' : 'comment',
                          'polymorphic_on' : type,
                      }
                  
                  
                  class PostComment(Comment):
                      __tablename__ = 'post_comment'
                  
                      id = Column(Integer, ForeignKey('comment.id'), primary_key=True)
                      ready = Column(Boolean)
                  
                      __mapper_args__= {
                          'polymorphic_identity' : 'post_comment',
                      }
                  
                  
                  
                  def main():
                      Base.metadata.drop_all(engine)
                      Base.metadata.create_all(engine)
                  
                      d1 = Document()
                      DBSession.add(d1)
                  
                      d2 = Document()
                      DBSession.add(d2)
                  
                      c1 = PostComment(document=d1, ready=True)
                      DBSession.add(c1)
                  
                      c2 = PostComment(document=d1, ready=True)
                      DBSession.add(c2)
                  
                      c3 = PostComment(document=d2, ready=True)
                      DBSession.add(c3)
                  
                      c4 = PostComment(document=d2, ready=True)
                      DBSession.add(c4)
                  
                      DBSession.commit()
                  
                      query = d1.comments.filter(PostComment.ready==True)
                  
                      print len(query.all())      # returns 2
                      print query.count()         # returns 8
                  
                  
                  if __name__ == '__main__':
                      main()
                  

                  更新

                  http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html#sqlalchemy.orm.query.Query.count

                  它说返回此查询将返回的行数.".

                  It says "Return a count of rows this Query would return.".

                  推荐答案

                  为什么结果是 8 而不是 2?因为您得到的查询是 笛卡尔积 (8 = 2 * 2 * 2).
                  反过来,发生这种情况是因为您有 with inheritancedynamic 关系,它从两个表(commentpost_comment) 之间没有任何谓词.

                  Why do you get 8 as result instead of 2? Because you get a query which is a cartesian product (8 = 2 * 2 * 2).
                  In turn, this happens because you have dynamic relationship with inheritance, which creates select from both tables (comment and post_comment) without any predicate between them.

                  为什么第一个查询只返回 2?好吧,因为您要求的是实际的映射实例,所以 sqlalchemy 足够聪明,可以过滤掉重复项,尽管底层 SQL 语句也确实返回 8 行.

                  Why the first query returns just 2? Well, because you are asking for actual mapped instances, sqlalchemy is smart enough to filter out the duplicates, although the underlying SQL statement does return 8 rows as well.

                  在查询中添加 join 以解决此问题:

                  Add a join to your query to fix this:

                  query = d1.comments.join(PostComment).filter(PostComment.ready == True)
                  

                  这篇关于sqlalchemy 与 `len(query.all())` 和 `query.count()` 的值不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 库)
                    • <bdo id='1Mjti'></bdo><ul id='1Mjti'></ul>
                      <tfoot id='1Mjti'></tfoot>
                      <i id='1Mjti'><tr id='1Mjti'><dt id='1Mjti'><q id='1Mjti'><span id='1Mjti'><b id='1Mjti'><form id='1Mjti'><ins id='1Mjti'></ins><ul id='1Mjti'></ul><sub id='1Mjti'></sub></form><legend id='1Mjti'></legend><bdo id='1Mjti'><pre id='1Mjti'><center id='1Mjti'></center></pre></bdo></b><th id='1Mjti'></th></span></q></dt></tr></i><div id='1Mjti'><tfoot id='1Mjti'></tfoot><dl id='1Mjti'><fieldset id='1Mjti'></fieldset></dl></div>
                        <tbody id='1Mjti'></tbody>
                    • <legend id='1Mjti'><style id='1Mjti'><dir id='1Mjti'><q id='1Mjti'></q></dir></style></legend>

                            <small id='1Mjti'></small><noframes id='1Mjti'>