Skip to content

Subqueryloader - #330

Open
jekel wants to merge 1 commit into
python-gino:masterfrom
jekel:subqueryloader
Open

Subqueryloader#330
jekel wants to merge 1 commit into
python-gino:masterfrom
jekel:subqueryloader

Conversation

@jekel

@jekeljekel commented Sep 6, 2018

Copy link
Copy Markdown
Contributor

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

@coveralls

coveralls commented Sep 6, 2018

Copy link
Copy Markdown

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%

TotalsCoverage Status
Change from base Build 1209:0.008%
Covered Lines:3987
Relevant Lines:4057

💛 - Coveralls

@wwwjfy

Copy link
Copy Markdown
Member

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.

@jekel

jekel commented Sep 9, 2018

Copy link
Copy Markdown
ContributorAuthor

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?

@wwwjfy

Copy link
Copy Markdown
Member

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.

@jekel

jekel commented Sep 9, 2018

Copy link
Copy Markdown
ContributorAuthor

@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

@wwwjfy

Copy link
Copy Markdown
Member

Exactly. That's the cases I'd like to explore, so as to give users straightforward loader and least surprise. Thanks 😊

@jekel
jekelforce-pushed the subqueryloader branch 2 times, most recently from a8cfb1d to 3f2356bCompareSeptember 19, 2018 16:21
@jekel

Copy link
Copy Markdown
ContributorAuthor

Hi @wwwjfy , how is going your research?

@wwwjfy

Copy link
Copy Markdown
Member

Sorry for the silence!

I just picked up a few issues again and will work out something this weekend.

@fantixfantix added this to the v1.1.x milestone Oct 19, 2018
@wwwjfy

Copy link
Copy Markdown
Member

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.

@wwwjfy

Copy link
Copy Markdown
Member

I don't know if this can meet your needs. If not, an example will be very helpful.

@jekel

Copy link
Copy Markdown
ContributorAuthor

@wwwjfy i have found the case your code does not cover - complete model loading from subquery without specifying all columns explicitly

@wwwjfy

Copy link
Copy Markdown
Member

@jekel I could achieve this:

asyncdeftest(user):
ua=User.alias()
query=select([ua]).alias()
outer_query=select([text('1'), query])
result=awaitouter_query.gino.load(ua).all()
assertlen(result) ==1assertresult[0].id==user.id

Is it what you said?

@fantix
fantixforce-pushed the master branch 4 times, most recently from d3bba04 to 79e7d4cCompareDecember 27, 2019 07:42
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@jekel@coveralls@wwwjfy@fantix