Python Sqlalchemy-我们可以在关系定义中使用日期比较吗?
我已定义此映射器:Python Sqlalchemy-我们可以在关系定义中使用日期比较吗?,python,database,orm,sqlalchemy,mapper,Python,Database,Orm,Sqlalchemy,Mapper,我已定义此映射器: mapper(Resource, resource_table, properties = {'type' : relation(ResourceType,lazy = False), 'groups' : relation(Group, secondary = model.tables['resource_group'], backref = 'resources'), 'parent' : relation(Relation, uselist=Fa
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()))})
但由于某些原因,如果我在关系表中创建新行,并将关系中旧行的结束日期更改为旧日期,则不会更新属性父级。
此外,如果重新加载资源行,将显示与旧日期的旧关系,因此我非常确定这与映射器中的日期比较有关
如果我用标志列字符串或整数替换结束日期,并对标志进行比较,我会得到正确的行为,但我确实希望使用日期
欢迎任何帮助
谢谢
理查德·洛佩斯(Richard Lopes)我已经找到了问题所在。 这种关系实际上正在发挥作用。 通过将end_date设置为datetime.now()-1秒的方式解决了这个问题,因此它发生在SQLAlchemy实际刷新资源之前。 我想这是个问题
Richard Lopes实际上,如果您运行的代码耗时超过1秒,则此解决方案不够可靠。因此,您需要投入更多的时间,或者依赖于结束日期顶部的布尔标志,这是更安全的选择。