[英]SQLAlchemy - Problem with an association table and dates in primary join
我正在使用SQLAlchemy定义映射,除一件事情外,我几乎完成了所有工作。 我有一个“资源”对象和一个关联表“关系”,它具有几个属性以及两个资源之间的关系。 到目前为止,我几乎一直在尝试成功完成的工作是在资源对象上提供2个属性:父级和子级遍历关联表存储的树。 2个属性之间的关系仅持续一段时间,因此存在开始日期和结束日期。 一次只能有一个资源成为另一资源的父项。
我的问题是,如果我终止一个关系并创建一个新关系,则不会刷新父属性。 我在想,资源的父属性的primaryjoin可能存在问题。
这是一些代码:
resource_table = model.tables['resource']
relation_table = model.tables['resource_relation']
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 > func.now())),
'children' : relation(Relation,
primaryjoin = and_(
relation_table.c.parent_id == resource_table.c.res_id,
relation_table.c.end_date > func.now()))
}
)
mapper(Relation, relation_table,
properties = {
'resource' : relation(Resource,
primaryjoin = (relation_table.c.res_id == resource_table.c.res_id)),
'parent' : relation(Resource,
primaryjoin = (relation_table.c.parent_id == resource_table.c.res_id))
}
)
oldrelation = resource.parent
oldrelation.end_date = datetime.today()
relation = self.createRelation(parent, resource)
# Here the relation object has not replaced oldrelation in the resource object
任何想法 ?
谢谢,
理查德·洛佩斯
考虑在日期比较中使用>=
而不是>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.