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

Allow ordering by columns in outside tables via JOIN #6008

Closed
vadimdemedes opened this issue Oct 26, 2015 · 2 comments
Closed

Allow ordering by columns in outside tables via JOIN #6008

vadimdemedes opened this issue Oct 26, 2015 · 2 comments
Labels
affects:api Affects the Ghost API later [triage] Things we intend to work but are not immediate priority

Comments

@vadimdemedes
Copy link

This issue is related to #5602 and #5989.

Issue

So, while implementing support for ordering by rows from outside tables (when using join), I discovered an issue with the way Bookshelf fetches records. It does not use join, but uses extra select query to fetch additional rows from other tables and then merges them into the final result.

For example, I want to fetch posts and include author information in them:

PostAPI.browse({include: 'author'});

I'd expect a select query with a join statement. But here's what happens instead:

screen shot 2015-10-26 at 12 56 04 pm

It finds posts, then looks for author_id and finds that there are 3 unique author_ids. After that, it executes one more select with those ids. When that extra query is finished, author results get merged into posts result (into author column).

Consequences

Because of this issue, it is not possible to order by "joined" values, e.g. author.name. Because the initial query with order by does not know what author column is:

select "posts.*" from "posts" order by "title" DESC

Solution

The proposed solution is to detect, if order references any columns in outside tables. If it does, modify the query to join that outside table. In that case, queries with order will work as expected.

Note: Similar work is done for filter in core/server/models/base/utils.js. If join is required, it modifies a query to include these statements.

@sebgie
Copy link
Contributor

sebgie commented Oct 26, 2015

I have not tried it out yet, but this SO question shows a possible approach to sorting by related model values. http://stackoverflow.com/questions/22568153/order-bookshelf-js-fetch-by-related-column-value

@ErisDS
Copy link
Member

ErisDS commented Oct 26, 2015

Link to the discussion that lead to us breaking this issue out from #5602: https://ghost.slack.com/archives/dev/p1445860844002641

@vadimdemedes
Copy link
Author

@sebgie Thanks for the link, I'll try that.

@ErisDS ErisDS added the affects:api Affects the Ghost API label Nov 2, 2015
@ErisDS ErisDS added this to the Public API v1 milestone Nov 2, 2015
@ErisDS ErisDS modified the milestone: Public API v1 Dec 3, 2015
@ErisDS ErisDS added the later [triage] Things we intend to work but are not immediate priority label Sep 20, 2016
@ErisDS
Copy link
Member

ErisDS commented Sep 20, 2016

I'm closing most API issues temporarily with the later label.

JSON API Overhaul & OAuth access are currently scheduled next on the roadmap

@ErisDS ErisDS closed this as completed Sep 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects:api Affects the Ghost API later [triage] Things we intend to work but are not immediate priority
Projects
None yet
Development

No branches or pull requests

3 participants