Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subqueryloader #330

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gino/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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 @@ -204,6 +204,27 @@ async def test_loader_with_aggregation(user):
assert user_count is None


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