我正在尝试使用SQL alchemy的按日期的查询限制。示例代码如下:
def young_puppies(session=get_session()):
"""Query all of the puppies.
Return those that are less than 6 months old organized by
the youngest first.
"""
young = []
today = datetime.date.today()
sixm = today - relativedelta(months=-6)
sel = session.query(Puppy).\
filter(Puppy.dateOfBirth.between(sixm, today)).\
order_by(desc(Puppy.dateOfBirth))
print (sel)
res = session.execute(sel)
for row in res:
young.append(dict(row))
session.close
return young
但是,查询生成出现意外行为:
SELECT puppy.name AS puppy_name, puppy.id AS puppy_id, puppy."dateOfBirth" AS "puppy_dateOfBirth", puppy.gender AS puppy_gender, puppy.picture AS puppy_picture, puppy.weight AS puppy_weight, puppy.shelter_id AS puppy_shelter_id
FROM puppy
WHERE puppy."dateOfBirth" BETWEEN ? AND ? ORDER BY puppy."dateOfBirth" DESC
即使在插入过滤器之前将类型强制转换为str,也会显示相同的行为。我不知道为什么会发生这种情况,或者是否有解决办法。根据我所做的研究,这应该行得通。这个?而不是限制的日期。你知道吗
编辑:
奇怪的是,我在第六个月的约会上做了一个刺痛的动作:
def young_puppies(session=get_session()):
"""Query all of the puppies.
Return those that are less than 6 months old organized by
the youngest first.
"""
young = []
tday = datetime.date.today()
sixm = tday - relativedelta(months=-6)
sel = session.query(Puppy).\
filter(Puppy.dateOfBirth.
between(sixm.strftime('%y-%m-%d'), tday)).\
order_by(desc(Puppy.dateOfBirth))
print (sel)
res = session.execute(sel)
for row in res:
young.append(dict(row))
session.close
return young
目前没有回答
相关问题 更多 >
编程相关推荐