<tfoot id='OJ4s3'></tfoot>

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

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

        SQLAlchemy:使用“and"和“or"时出现意外结果

        SQLAlchemy: unexpected results when using `and` and `or`(SQLAlchemy:使用“and和“or时出现意外结果)
          <i id='onnMm'><tr id='onnMm'><dt id='onnMm'><q id='onnMm'><span id='onnMm'><b id='onnMm'><form id='onnMm'><ins id='onnMm'></ins><ul id='onnMm'></ul><sub id='onnMm'></sub></form><legend id='onnMm'></legend><bdo id='onnMm'><pre id='onnMm'><center id='onnMm'></center></pre></bdo></b><th id='onnMm'></th></span></q></dt></tr></i><div id='onnMm'><tfoot id='onnMm'></tfoot><dl id='onnMm'><fieldset id='onnMm'></fieldset></dl></div>

            <tfoot id='onnMm'></tfoot>

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

              <bdo id='onnMm'></bdo><ul id='onnMm'></ul>

                • <legend id='onnMm'><style id='onnMm'><dir id='onnMm'><q id='onnMm'></q></dir></style></legend>
                    <tbody id='onnMm'></tbody>

                  本文介绍了SQLAlchemy:使用“and"和“or"时出现意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个声明性基类News:

                  class News(Base):
                      __tablename__ = "news"
                      id = Column(Integer, primary_key = True)
                      title = Column(String)
                      author = Column(String)
                      url = Column(String)
                      comments = Column(Integer)
                      points = Column(Integer)
                      label = Column(String)
                  

                  我还有一个函数 f(title),它获取一个字符串并返回 3 个字符串变体之一:'good'、'maybe' 或 'never'.我尝试获取过滤后的行:

                  I also have a function f(title), that gets a string and returns one of 3 variants of strings: 'good', 'maybe' or 'never'. I try to get filtered rows:

                  rows = s.query(News).filter(News.label == None and f(News.title) == 'good').all()
                  

                  但程序失败,引发此错误:

                  But the program fails, raising this error:

                  raise TypeError("Boolean value of this clause is not defined")
                  

                  我该如何解决?

                  推荐答案

                  问题是这样的:

                  News.label == None and f(News.title) == 'good'
                  #                  ^^^ here
                  

                  Python 不允许覆盖布尔 操作 andor 的行为.您可以通过 __bool__<在一定程度上影响它们/a> 在 Python 3 和 __nonzero__ 在 Python 2 中,但所做的只是它 定义对象的真值.

                  Python does not allow overriding the behaviour of boolean operations and and or. You can influence them to some extent with __bool__ in Python 3 and __nonzero__ in Python 2, but all that does is that it defines the truth value of your object.

                  如果有问题的对象没有实现 __bool__ 并抛出错误,或者没有抛出实现,那么由于 andor 的短路特性:

                  If the objects in question had not implemented __bool__ and thrown the error, or the implementation had not thrown, you would've gotten possibly rather cryptic errors due to the short-circuiting nature of and and or:

                  In [19]: (News.label == 'asdf') and True
                  Out[19]: <sqlalchemy.sql.elements.BinaryExpression object at 0x7f62c416fa58>
                  
                  In [24]: (News.label == 'asdf') or True
                  Out[24]: True
                  

                  因为

                  In [26]: bool(News.label == 'asdf')
                  Out[26]: False
                  

                  这可能并且会导致以错误的 SQL 表达式形式出现问题:

                  This could and would lead to hair pulling in the form of incorrect SQL expressions:

                  In [28]: print(News.label == 'asdf' or News.author == 'NOT WHAT YOU EXPECTED')
                  news.author = :author_1
                  

                  要生成布尔 SQL 表达式,请使用 and_(), or_()not_() sql 表达式函数,或二进制 &, |~ 运算符重载:

                  To produce boolean SQL expressions either use the and_(), or_(), and not_() sql expression functions, or the binary &, |, and ~ operator overloads:

                  # Parentheses required due to operator precedence
                  filter((News.label == None) & (f(News.title) == 'good'))
                  

                  filter(and_(News.label == None, f(News.title) == 'good'))
                  

                  或将多个条件传递给对 Query.filter():

                  or pass multiple criterion to a call to Query.filter():

                  filter(News.label == None, f(News.title) == 'good')
                  

                  或组合对 filter() 的多个调用:

                  or combine multiple calls to filter():

                  filter(News.label == None).filter(f(News.title) == 'good')
                  

                  这篇关于SQLAlchemy:使用“and"和“or"时出现意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <bdo id='Czi7z'></bdo><ul id='Czi7z'></ul>

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

                        <tbody id='Czi7z'></tbody>
                    1. <legend id='Czi7z'><style id='Czi7z'><dir id='Czi7z'><q id='Czi7z'></q></dir></style></legend>

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