Support for many-to-one batch fetching

This commit is contained in:
Konsta Vesterinen
2013-08-21 15:20:31 +03:00
parent b95736cec1
commit 03b1985980
5 changed files with 30 additions and 9 deletions

View File

@@ -4,6 +4,12 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Utils release.
0.16.9 (2013-08-21)
^^^^^^^^^^^^^^^^^^^
- Support for many-to-one directed relationship properties batch fetching
0.16.8 (2013-08-21)
^^^^^^^^^^^^^^^^^^^

View File

@@ -55,7 +55,7 @@ for name, requirements in extras_require.items():
setup(
name='SQLAlchemy-Utils',
version='0.16.8',
version='0.16.9',
url='https://github.com/kvesteri/sqlalchemy-utils',
license='BSD',
author='Konsta Vesterinen',

View File

@@ -37,7 +37,7 @@ from .types import (
)
__version__ = '0.16.8'
__version__ = '0.16.9'
__all__ = (

View File

@@ -156,7 +156,9 @@ class Fetcher(object):
self.entities = coordinator.entities
self.first = self.entities[0]
self.session = object_session(self.first)
self.init_parent_dict()
def init_parent_dict(self):
self.parent_dict = dict(
(self.local_values(entity), [])
for entity in self.entities
@@ -237,6 +239,12 @@ class ManyToManyFetcher(Fetcher):
class ManyToOneFetcher(Fetcher):
def init_parent_dict(self):
self.parent_dict = dict(
(self.local_values(entity), None)
for entity in self.entities
)
def fetch(self):
column_name = list(self.prop.remote_side)[0].name
@@ -248,9 +256,7 @@ class ManyToOneFetcher(Fetcher):
)
for entity in self.related_entities:
self.parent_dict[getattr(entity, column_name)].append(
entity
)
self.parent_dict[getattr(entity, column_name)] = entity
class OneToManyFetcher(Fetcher):

View File

@@ -28,17 +28,25 @@ class TestBatchFetchManyToOneRelationships(TestCase):
def setup_method(self, method):
TestCase.setup_method(self, method)
self.users = [
self.User(id=333, name=u'John'),
self.User(id=334, name=u'Matt')
]
articles = [
self.Article(
id=1,
name=u'Article 1',
author=self.User(id=333, name=u'John')
author=self.users[0]
),
self.Article(
id=2,
name=u'Article 2',
author=self.User(id=334, name=u'Matt')
author=self.users[1]
),
self.Article(
id=3,
name=u'Article 3'
)
]
self.session.add_all(articles)
self.session.commit()
@@ -50,6 +58,7 @@ class TestBatchFetchManyToOneRelationships(TestCase):
'author'
)
query_count = self.connection.query_count
assert articles[0].author # no lazy load should occur
assert articles[1].author # no lazy load should occur
assert articles[0].author == self.users[0] # no lazy load should occur
assert articles[1].author == self.users[1] # no lazy load should occur
assert articles[2].author is None # no lazy load should occur
assert self.connection.query_count == query_count