我想将SQL转移到SQLAlchemy,并且我已经嵌套了case语句。
简单的案例正在发挥作用:
stmt = sqlalchemy.select([self.tusg_view_specials]).where(
sqlalchemy.case([
(self.tusg_view_specials.c.webo_close_date >= (datetime.date.today() - datetime.timedelta(days=30)), 1),
(self.tusg_view_specials.c.wo_closed_date >= (datetime.date.today() - datetime.timedelta(days=61)), 1)
], else_=0),
)
但是当我有一个嵌套的情况时,意味着THEN是一个case子句而不是一个简单的值:
stmt = sqlalchemy.select([self.tusg_view_specials]).where(
sqlalchemy.case([
(self.tusg_view_specials.c.webo_close_date >= (datetime.date.today() - datetime.timedelta(days=30)), 1),
(self.tusg_view_specials.c.wo_closed_date >= (datetime.date.today() - datetime.timedelta(days=61)), 1),
(self.tusg_view_specials.c.work_order_number is None,
sqlalchemy.case([(self.tusg_view_specials.c.flag_is_abw == 1, 1)], else_=0))
], else_=0), <<-- This line is shown to cause the error
)
我收到以下错误消息,我不知道如何处理它:
sqlalchemy.exc.ArgumentError: Ambiguous literal: False. Use the 'text()' function to indicate a SQL expression literal, or 'literal()' to indicate a bound value.
我可以阅读文字,但不知道如何解读。搜索结果&#34;嵌套案例&#34;在SQLAlchemy上几乎没有。
答案 0 :(得分:0)
正如IljaEveilä所写,原因是“IS无”,需要由
取代table_instance.c.work_order_number.is_(None)
或
table_instance.c.work_order_number == None