开发者

Sqlalchemy - Can we use date comparison in relation definition?

开发者 https://www.devze.com 2022-12-22 03:42 出处:网络
I have this mapper defined: mapper(Resource, resource_table, properties = {\'type\' : relation(ResourceType,lazy = False),

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_dat开发者_运维技巧e > 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号