打印出查询生成的实际sql以查看发生了什么。例如。:
ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))
print(ld)
印刷品:
SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
FROM lunch_day
WHERE DATE(lunch_day.timestamp = %(timestamp_1)s) AND lunch_day.status = %(status_1)s
在那里你可以看到
lunch_day.timestamp
timestamp_1
正在传递给
DATE
功能。
这在sqlalchemy查询中非常容易看到:
func.DATE(LunchDay.timestamp == datetime.date.today())
. 我想你是想改变信仰
LunchDay.timestamp
约会
然后
与之相比
datetime.date.today()
应该是哪一个
db.func.DATE(LunchDay.timestamp) == date.today()
.
print(LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)))
印刷品:
SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
FROM lunch_day
WHERE DATE(lunch_day.timestamp) = %(DATE_1)s AND lunch_day.status = %(status_1)s
另一件需要注意的事情是,多个语句传递给
filter()
被自动视为
and_()
,因此您可以通过删除以下内容来简化查询:
LunchDay.query.filter(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)