Subqueryloader
SubqueryLoader is used to load Models from sqlalchemy aliased queries, opposite of #326 which works only on aliased gino Models Common usecase for this loader will be - when you will have complex query with subquery, aggregated columns, etc... and you need to get result as Model object defined inside subquery
Previus discussion can be found here #323
Pull Request Test Coverage Report for Build 1214
- 19 of 19 (100.0%) changed or added relevant lines in 2 files are covered.
- No unchanged relevant lines lost coverage.
- Overall coverage increased (+0.008%) to 98.275%
| Totals | |
|---|---|
| Change from base Build 1209: | 0.008% |
| Covered Lines: | 3987 |
| Relevant Lines: | 4057 |
💛 - Coveralls
As discussed in #323, I'm not convinced that this is the complete solution. The use case in the unit test can be achieved in the latest version of Gino already. We do not need model and corresponding_column to get the column in subquery, as columns property of the subquery already has everything, as in https://github.com/fantix/gino/pull/326/files#diff-f6247715790dd30f031c9c3d67d5814eR148.
We do need better support for SQLAlchemy subqueries, or a wrapper. I'll come up with a good use case first.
We do need better support for SQLAlchemy subqueries, or a wrapper
Why not? New users may need this functionality when porting their application on asyncio.
I'll come up with a good use case first.
Can you show me please also how can i achive the same without using corresponding_column in new use cases?
We "do" need, not we "don't" need. 😂
alias.corresponding_column(User.id) can be replaced by alias.columns.id, so we don't need User to be passed to the loader. I'm not saying it can cover all cases, but at least part of them.
@wwwjfy Sorry, i have read it wrong :)
alias.corresponding_column(User.id) can be replaced by alias.columns.id
what happens when there will be several models inside subquery? please take it mind when creating new use case
Exactly. That's the cases I'd like to explore, so as to give users straightforward loader and least surprise. Thanks 😊
Hi @wwwjfy , how is going your research?
Sorry for the silence!
I just picked up a few issues again and will work out something this weekend.
I created #365, trying to show what can be done in the description of this issue, for subqueries and aggregate functions. Basically, it won't be hard as long as we keep the reference of columns and/or models in a subquery and pass them to loaders.
I don't know if this can meet your needs. If not, an example will be very helpful.
@wwwjfy i have found the case your code does not cover - complete model loading from subquery without specifying all columns explicitly
@jekel I could achieve this:
async def test(user):
ua = User.alias()
query = select([ua]).alias()
outer_query = select([text('1'), query])
result = await outer_query.gino.load(ua).all()
assert len(result) == 1
assert result[0].id == user.id
Is it what you said?