unnecessary lazy loading of backref attribute
Issue #632
resolved
this test
class TestBackrefLoad(AssertMixin):
def setUpAll(self):
global metadata
metadata = MetaData(testbase.db)
self.a_table = Table("a", metadata,
Column("id", Integer(), primary_key=True),
Column("name", String()),
)
self.a_table.create()
self.b_table = Table("b", metadata,
Column("id", Integer(), primary_key=True),
Column("a_id", Integer(), ForeignKey("a.id"))
)
self.b_table.create()
def tearDownAll(self):
self.b_table.drop()
self.a_table.drop()
def testbasic(self):
class A(object):
def __init__(self,id):
self.id = id
class B(object):
def __init__(self,id):
self.id = id
mapper(B,self.b_table,always_refresh = True)
mapper(A, self.a_table, properties={
'b_list': relation(B,private=True,backref = 'a_link')},always_refresh = True)
#First, populate some data
sess= create_session()
a= A(1)
sess.save(a)
a.name = 'test'
b = B(10)
sess.save(b)
a.b_list.append(b)
sess.flush()
sess.close()
#Now, get instance and change it name
sess1 = create_session()
a = sess1.query(A).get(1)
a.name = 'test2'
c = a.b_list[0](0).a_link#Lazy loading a_link will change a.name to 'test'
assert a.name == 'test2'
sess1.close
shows what getting backref attribute will fire lazy loading operation and interferes with session editing.
Comments (3)
-
Account Deleted -
Account Deleted I work with postgres.
-
repo owner - changed status to wontfix
the "always_refresh" flag is an antipattern which is not supported. use session.expire() / session.expunge() / session.refresh() / session.clear() to refresh the state of objects from the database.
- Log in to comment
Without always_refresh attribute in mapper, all works fine. But I need it in order to periodically refresh my data, changing by external application.