From: Mike Bayer Date: Tue, 15 Nov 2005 04:31:48 +0000 (+0000) Subject: lazyloader re-attaches lazy load upon new object create X-Git-Tag: rel_0_1_0~342 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ac220dd2248dc29b5aa3a73b179af8fe851d3be2;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git lazyloader re-attaches lazy load upon new object create --- diff --git a/lib/sqlalchemy/mapper.py b/lib/sqlalchemy/mapper.py index b493b7522e..8f8161108a 100644 --- a/lib/sqlalchemy/mapper.py +++ b/lib/sqlalchemy/mapper.py @@ -418,6 +418,7 @@ class Mapper(object): def _getattrbycolumn(self, obj, column): try: prop = self.columntoproperty[column] + #print "prop: ", repr(prop[0]), prop[0].key except KeyError: try: prop = self.props[column.key] @@ -983,6 +984,8 @@ class LazyLoader(PropertyLoader): (self.lazywhere, self.lazybinds) = create_lazy_clause(self.parent.table, self.primaryjoin, self.secondaryjoin, self.foreignkey, self.direction) def _set_class_attribute(self, class_, key): + # sets an attribute on the mapped class, which will call setup_loader off this object when the attribute is first accessed. + # setup_loader then creates a "lazyload" callable that is called second. objectstore.uow().register_attribute(class_, key, uselist = self.uselist, deleteremoved = self.private, create_prop=lambda i: self.setup_loader(i)) def setup_loader(self, instance): @@ -1019,11 +1022,12 @@ class LazyLoader(PropertyLoader): def execute(self, instance, row, identitykey, imap, isnew): if isnew: - return - # lazyload = self.setup_loader(instance) - # objectstore.uow().register_callable(instance, self.key, lazyload, uselist=self.uselist, deleteremoved = self.private) - # self.setup_loader(instance) - + # when new rows are processed, we re-set a lazyloader on the instance. this is because the constructors + # of an object might try to access its lazy-properties, which will result in nothing being returned + # since we havent mapped anything into the instance yet. + lazyload = self.setup_loader(instance) + objectstore.uow().register_callable(instance, self.key, lazyload, uselist=self.uselist, deleteremoved = self.private) + def create_lazy_clause(table, primaryjoin, secondaryjoin, foreignkey, direction): binds = {} def visit_binary(binary):