diff --git a/gino/loader.py b/gino/loader.py index 835d9178..e089c04a 100644 --- a/gino/loader.py +++ b/gino/loader.py @@ -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) diff --git a/tests/test_loader.py b/tests/test_loader.py index 07ed6001..cfb920de 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -161,6 +161,27 @@ async def test_alias_loader_columns(user): assert u.id is not 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(