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 更改为旧日期,则属性父级不会更新。 此外,如果重新加载资源行,则会显示与旧日期的旧关系,因此我非常确定它与映射器中的日期比较有关。
如果我用标志列字符串或整数替换 end_date 并对标志进行比较,我会得到正确的行为,但我确实想使用日期。
欢迎任何帮助。
谢谢,
理查德·洛佩斯
I have this mapper defined:
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()))})
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.
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.
Any help is welcome.
Thanks,
Richard Lopes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上已经发现了问题所在。
这种关系实际上正在发挥作用。
通过将 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.
Richard Lopes