2010-03-04 60 views
1

我正在使用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 

任何想法?

感謝,

理查德·洛佩斯

+0

不要讓你的潛在幫手使用水平滾動條:o) – 2010-03-04 07:38:12

+0

當然。現在看起來好多了。謝謝 ;-)。 – 2010-03-04 21:08:12

回答

0

我居然發現我麻煩的原因,並得到它的工作。 See here

乾杯,

理查德·洛佩斯

0

考慮日期比較使用>=代替>