SQLAlchemy-我们可以在关系定义中使用日期比较吗? [英] Sqlalchemy - Can we use date comparison in relation definition?

查看:94
本文介绍了SQLAlchemy-我们可以在关系定义中使用日期比较吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已定义了此映射器:

mapper(Resource, resource_table,
 properties = {'type' : relation(ResourceType,lazy = False),
  'groups' : relation(Group, secondary = model.tables['resource_group'], 
      backref = 'resources'),
  'parent' : relation(Relation, uselist=False, primaryjoin = 
      and_(relation_table.c.res_id == resource_table.c.res_id, 
      relation_table.c.end_date > datetime.now())),
  'children' : relation(Relation, primaryjoin = 
      and_(relation_table.c.parent_id == resource_table.c.res_id, 
      relation_table.c.end_date > func.now()))})

但是由于某些原因,如果我在关系表中创建新行并将关系中旧行的end_date更改为旧日期,则不会更新属性parent.另外,如果重新加载资源行,则会显示具有旧日期的旧关系,因此,我很确定这与映射器中的日期比较有关.

But for some reason, if I create a new row in the relation table and change the end_date of the old row in the relation to an old date, the property parent is not updated. Also if a reload the resource row, the old relation with the old date is displayed, so I am pretty sure it has to do with the date comparison in the mapper.

如果我用一个标志列字符串或整数替换end_date并在该标志上进行比较,我会得到正确的行为,但是我确实想使用日期.

If I replace the end_date by a flag column string or integer and do a comparison on the flag I get the proper behaviour, but I do want to use dates.

欢迎任何帮助.

谢谢

理查德·洛普斯

推荐答案

我实际上发现了问题所在.该关系实际上是有效的.通过将end_date设置为类似datetime.now()的方式解决了该问题-1秒,因此发生在SQLAlchemy实际刷新资源之前.我想是毫秒级的问题.

I have actually found what was wrong. The relation is actually working. The problem was solved by setting the end_date to something like datetime.now() - 1 second, so it happens before the resource is actually refreshed by SQLAlchemy. A milliseconds issue I suppose.

理查德·洛普斯

这篇关于SQLAlchemy-我们可以在关系定义中使用日期比较吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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