From 3715e10bf82786920bf8c018a99221f0d1713b3d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 7 Dec 2007 16:31:12 +0000 Subject: [PATCH] - also with dynamic, implemented correct count() behavior as well as other helper methods. --- CHANGES | 3 +++ lib/sqlalchemy/orm/dynamic.py | 15 +++++++++++++-- test/orm/dynamic.py | 8 ++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index ae64f385cb..2e1160c201 100644 --- a/CHANGES +++ b/CHANGES @@ -57,6 +57,9 @@ CHANGES that of regular relations. if passive_deletes flag (also just added) is not set, a delete of the parent item will trigger a full load of the child items so that they can be deleted or updated accordingly. + + - also with dynamic, implemented correct count() behavior as well + as other helper methods. - query.get() and query.load() do not take existing filter or other criterion into account; these methods *always* look up the given id diff --git a/lib/sqlalchemy/orm/dynamic.py b/lib/sqlalchemy/orm/dynamic.py index f5662a933d..e55703c420 100644 --- a/lib/sqlalchemy/orm/dynamic.py +++ b/lib/sqlalchemy/orm/dynamic.py @@ -74,7 +74,11 @@ class AppenderQuery(Query): return None else: return sess - + + def _get_session(self): + return self.__session() + session = property(_get_session) + def __iter__(self): sess = self.__session() if sess is None: @@ -88,7 +92,14 @@ class AppenderQuery(Query): return self.attr.get_history(self.state, passive=True)._added_items.__getitem__(index) else: return self._clone(sess).__getitem__(index) - + + def count(self): + sess = self.__session() + if sess is None: + return len(self.attr.get_history(self.state, passive=True)._added_items) + else: + return self._clone(sess).count() + def _clone(self, sess=None): # note we're returning an entirely new Query class instance here # without any assignment capabilities; diff --git a/test/orm/dynamic.py b/test/orm/dynamic.py index 096d945781..fe0bc92d72 100644 --- a/test/orm/dynamic.py +++ b/test/orm/dynamic.py @@ -24,6 +24,14 @@ class DynamicTest(FixtureTest): assert [User(id=7, addresses=[Address(id=1, email_address='jack@bean.com')])] == q.filter(User.id==7).all() assert fixtures.user_address_result == q.all() + def test_count(self): + mapper(User, users, properties={ + 'addresses':dynamic_loader(mapper(Address, addresses)) + }) + sess = create_session() + u = sess.query(User).first() + assert u.addresses.count() == 1, u.addresses.count() + def test_backref(self): mapper(Address, addresses, properties={ 'user':relation(User, backref=backref('addresses', lazy='dynamic')) -- 2.47.3