列属性中的SQLalchemy和方法

时间:2013-09-29 17:12:48

标签: sqlalchemy

我有column_property来检查允许用户获得多少学分:

select(
    [func.ifnull( func.sum( orders_table.c.quantity ), 0 )],
    orders_table.c.user_id == users_table.c.id
).where( and_(
    orders_table.c.date_added < now_unix(),
    orders_table.c.date_expires > now_unix(),
    orders_table.c.status == STATUS_COMPLETED
) ).label( 'userAllowedCredits' ),
deferred = True

now_unix()方法返回当前的unix时间戳,但问题是此方法只加载一次,每次调用此userAllowedCredits属性时,查询将根据保存的相同初始值进行搜索当我的应用程序启动时。我需要这个now_unix()方法来返回每次调用时的实际当前时间戳。

我有意义吗?

1 个答案:

答案 0 :(得分:0)

您可能在启动时存储此表达式,因此now_unix()仅在此时执行。从SqlAlchemys的角度来看,它只是一个值。如何解决这个问题,取决于你的用例。您可以使用now(),它将使用now()函数中的数据库构建。如果您必须使用自己的now_unix(),则仍可将其作为参数传递。