Sqlalchemy - 我们可以在关系定义中使用日期比较吗?

发布于 2024-08-23 21:05:16 字数 811 浏览 0 评论 0原文

我定义了这个映射器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-08-30 21:05:16

我实际上已经发现了问题所在。
这种关系实际上正在发挥作用。
通过将 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文