SQLAlchemy此子句的布尔值未定义 [英] SQLAlchemy Boolean value of this clause is not defined

查看:3645
本文介绍了SQLAlchemy此子句的布尔值未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过SQLAlchemy创建的数据库News:

  class新闻(Base):
__tablename__ = 新闻
id = Column(Integer,primary_key = True)
title = Column(String)
author = Column(String)$ b $ url url = Column(String)
comments = Column(Integer)
points = Column(Integer)
label = Column(String)

我也有一个函数f(title),它得到一个字符串并返回3个字符串中的一个:好,可能或从不。
我尝试过滤行:

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

但是程序失败,引发这个错误:

$ p $ raise TypeError(这个子句的布尔值未定义)
code>

我该如何解决它?

解决方案

问题是这样的:

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

Python不允许覆盖布尔操作 。您可以在某种程度上使用 __ bool __ 在Python 3和 <$ c $在Python 2中使用c> __ nonzero __ ,但所做的只是它

如果有问题的对象没有执行 __ bool __ 并引发错误,您可能会因为和和或者的短路性质 =noreferrer>布尔操作和 - 或 -

 在[19]中:(News.label =='asdf')和True 
Out [19]:< sqlalchemy.sql.elements.BinaryExpression object at 0x7f62c416fa58>

[24]:(News.label =='asdf')or True
Out [24]:True
 在[26]中:bool(News.label = ='asdf')
Out [26]:False

这可能会导致以不正确的SQL表达式形式拉动头发:

 在[28]中:print(News.label =='asdf'或News.author =='不是你期望的')
news.author =:author_1

要生成布尔SQL表达式,请使用 和_() 或_() not _() sql表达式函数或二进制 & amp ; | 运算符重载:

 #由于运算符优先级所需的括号
filter((News.label == None)& (f(News.title)=='good'))

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

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

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

或者将多个调用合并为 filter()

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


I have a database "News" created via SQLAlchemy:

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)

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 program fails, raising this error:

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

How can I resovle it?

解决方案

The problem is this:

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

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.

If the objects in question had not implemented __bool__ and thrown the error, 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

because

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

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

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'))

or

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

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

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

or combine multiple calls to filter():

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

这篇关于SQLAlchemy此子句的布尔值未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆