I'm focusing on determining my mapping with SQLAlchemy and i'm virtually done except one factor. I've got a 'resource' object as well as an association table 'relation' with several qualities along with a relationship between 2 assets. What I've been attempting to do almost effectively to date, would be to provide around the resource object 2 qualities: parent and kids to traverse the tree saved through the association table. A relation between 2 qualities only last for some time, so there's a start and finish date. Just one resource could possibly be the parent of some other resource at any given time.

My problem is when I expire one relation and make up a brand new one, parents rentals are not rejuvenated. I'm thinking maybe there an problem using the primaryjoin for that parent property of resource.

Here's some code:

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

Any idea ?


Richard Lopes

I really have discovered the main reason of my trouble and first got it working. See here


Richard Lopes

Think about using >= rather than > in date comparison.