Skip to content

Commit

Permalink
Added SubqueryLoader to load model from query alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jekel committed Sep 19, 2018
1 parent 7d7b9c4 commit a8cfb1d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gino/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ def none_as_none(self, enabled=True):
return self


class SubqueryLoader(ModelLoader):
def __init__(self, model, subquery, **extras):
super().__init__(model, **extras)
self.columns = [subquery.corresponding_column(column) for column
in model]


class AliasLoader(ModelLoader):
def __init__(self, alias, *columns, **extras):
super().__init__(alias, *columns, **extras)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ def loader(row, context):
assert u.team.parent.name == user.team.parent.name


async def test_adjanency_list_on_nested_load(user):
subquery = db.select(User).alias()
base_query = subquery.outerjoin(Team).select()

query = base_query.execution_options(loader=(User.load('id')))
u = await query.gino.first()
# Because here arrives team_id, not user_id, and replaces it
assert u.id is None

from gino.loader import SubqueryLoader
query = base_query.execution_options(loader=SubqueryLoader(User, subquery, team=Team))
u = await query.gino.first()
assert u.id == user.id
assert u.realname == user.realname
assert u.nickname == user.nickname

assert isinstance(u.team, Team)
assert u.team.id == user.team.id
assert u.team.name == user.team.name


async def test_adjacency_list_query_builder(user):
group = Team.alias()
u = await User.load(team=Team.load(parent=group.on(
Expand Down

0 comments on commit a8cfb1d

Please sign in to comment.