David Xia
David Xia

Reputation: 5453

detached SQLalchemy session cannot lazy load backref objects

I'm trying to add a pylibmc memcached layer between sqlalchemy and my postgres DB. This post talked about how to do it. But I also want backrefs (for going from one to many in relationships), so I modified that post's user_classes.py to test it out.

I made UserStatus inherit from database.MemcachedORMObject and added a backref in the UserTable.mapper. But I get a "DetachedInstanceError: Parent instance is not bound to a Sessi on; lazy load operation of attribute 'user' cannot proceed" when trying to access a backref. I'm using Flask-SQLAlchemy==0.16, SQLAlchemy==0.8.0b2, and pylibmc==1.2.3.

class UserStatus(database.MemcachedORMObject):                                                      
    def __init__(self, name):                                                                      
        self.name = name                                                                            

UserStatusTable = Table('user_status', METADATA, \                                                  
    Column('user_status_id', Integer, primary_key=True),                                            
    Column('name', String)                                                                            )                                                                                                  
UserStatusTable.mapper = mapper(UserStatus, UserStatusTable)                                        
UserStatusTable.mapper.compile()                                                                    


class User(database.MemcachedORMObject):                                                            
    def __init__(self, name, email, password, user_status_id):                                      
        self.name = name                                                                            
        self.email = email                                                                          
        self.password = password                                                                    
        self.user_status_id = user_status_id                                                        

UserTable = Table('user', METADATA, \                                                              
    Column('user_id', Integer, primary_key=True),                                                  
    Column('name', String),
    Column('email', String),                                                                        
    Column('password', String),                                                                    
    Column('user_status_id', Integer),                                                              
    ForeignKeyConstraint(['user_status_id'], ['user_status.user_status_id'])                          )                                                                                                  
UserTable.mapper = mapper(User, UserTable,
  properties = { 'user_status': relation(UserStatus, backref='user', lazy=False)})                
UserTable.mapper.compile()

Error when trying to access a backref:

In [14]: from user_classes import *                                    In [15]: ust = UserStatus.fetch_by_field(UserStatus.user_status_id, 1) Memcached Getting: user_classes.UserStatus:(1)   In [16]: ust.user
--------------------------------------------------------------------------- DetachedInstanceError                     Traceback (most recent call last) <ipython-input-16-38fd625abed9> in <module>()
----> 1 ust.user /home/david/sqlalchemy-memcached/env/lib/python2.7/site-packages/sqlalchemy/orm/attribut es.pyc in __get__(self, instance, owner)
    249             return dict_[self.key]
    250         else:
--> 251             return self.impl.get(instance_state(instance), dict_)
    252
    253   /home/david/sqlalchemy-memcached/env/lib/python2.7/site-packages/sqlalchemy/orm/attribut es.pyc in get(self, state, dict_, passive)
    543                     value = callable_(passive)
    544                 elif self.callable_:
--> 545                     value = self.callable_(state, passive)
    546                 else:
    547                     value = ATTR_EMPTY   /home/david/sqlalchemy-memcached/env/lib/python2.7/site-packages/sqlalchemy/orm/strategi es.pyc in _load_for_state(self, state, passive)    495                 "Parent instance %s is not bound to a Session; "
    496                 "lazy load operation of attribute '%s' cannot proceed" %--> 497                 (orm_util.state_str(state), self.key)
    498             )
    499   DetachedInstanceError: Parent instance <UserStatus at 0x21e9dd0> is not bound to a Sessi on; lazy load operation of attribute 'user' cannot proceed

Upvotes: 2

Views: 2449

Answers (1)

schlamar
schlamar

Reputation: 9509

This is a bug in SQLAlchemy. I reported it on the bug tracker: http://www.sqlalchemy.org/trac/ticket/2743

You can always traverse the default relationship direction. So a workaround in your case would be to change the direction of the relationship (so you can access user from user status, but not the other way round).

A general workaround (if you need to traverse both directions later on) is to patch the __dict__ attribute in the object where the backref is defined. For the testcase at the bug tracker this would be:

for p in parents:
    for c in p.children:
        getattr(c, '__dict__')['parent'] = p
print parents[0].children[0].parent

Upvotes: 2

Related Questions