我正在使用SQLAlchemy定义我的映射,除了一件事情之外,我完成了大部分工作。 我有一个'资源'对象和一个关联表'关系'与几个属性和2资源之间的关系。 到目前为止,我一直试图做得几乎成功的是提供关于资源对象2的属性:父母和孩子遍历关联表存储的树。 2个属性之间的关系只持续一段时间,所以有一个开始和结束日期。一次只有一个资源可以成为另一个资源的父代。SQLAlchemy - 与主关联中的关联表和日期问题
我的问题是,如果我过期一个关系并创建一个新的,父属性不刷新。我想也许有一个与资源父资源的主要联接问题。
下面是一些代码:
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
任何想法?
感谢,
理查德·洛佩斯
不要让你的潜在帮手使用水平滚动条:o) – 2010-03-04 07:38:12
当然。现在看起来好多了。谢谢 ;-)。 – 2010-03-04 21:08:12