我正在使用SQLAlchemy定义我的映射,除了一件事我几乎完成了。 我有一个'资源'对象和一个关联表'关系'与几个属性和2资源之间的关系。 到目前为止,我几乎成功地做了几件事,就是在资源对象上提供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
有什么想法吗?
谢谢,
Richard Lopes