SQLAlchemy - 主要联接中的关联表和日期出现问题

时间:2010-03-04 06:08:18

标签: python database orm mapping sqlalchemy

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

2 个答案:

答案 0 :(得分:0)

我实际上找到了我的麻烦的原因,并让它工作。 See here

干杯,

Richard Lopes

答案 1 :(得分:0)

在日期比较中考虑使用>=代替>