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

@jekel jekel 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%

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

💛 - Coveralls

@wwwjfy

wwwjfy commented Sep 9, 2018

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
Contributor Author

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

wwwjfy commented Sep 9, 2018

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
Contributor Author

@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

wwwjfy commented Sep 9, 2018

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 jekel force-pushed the subqueryloader branch 2 times, most recently from a8cfb1d to 3f2356b Compare September 19, 2018 16:21
@jekel

jekel commented Oct 17, 2018

Copy link
Copy Markdown
Contributor Author

Hi @wwwjfy , how is going your research?

@wwwjfy

wwwjfy commented Oct 17, 2018

Copy link
Copy Markdown
Member

Sorry for the silence!

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

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

wwwjfy commented Oct 21, 2018

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

wwwjfy commented Oct 21, 2018

Copy link
Copy Markdown
Member

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

@jekel

jekel commented Oct 23, 2018

Copy link
Copy Markdown
Contributor Author

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

@wwwjfy

wwwjfy commented Oct 24, 2018

Copy link
Copy Markdown
Member

@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?

@fantix fantix force-pushed the master branch 10 times, most recently from 684f7b1 to 3969c27 Compare February 1, 2019 16:29
@fantix fantix force-pushed the master branch 4 times, most recently from d3bba04 to 79e7d4c Compare December 27, 2019 07:42
Sign up for free to 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