Allow ordering by columns in outside tables via JOIN #6008
Labels
affects:api
Affects the Ghost API
later
[triage] Things we intend to work but are not immediate priority
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 usejoin
, but uses extraselect
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:
I'd expect a
select
query with ajoin
statement. But here's what happens instead:It finds posts, then looks for
author_id
and finds that there are 3 uniqueauthor_id
s. After that, it executes one moreselect
with those ids. When that extra query is finished, author results get merged into posts result (intoauthor
column).Consequences
Because of this issue, it is not possible to order by "joined" values, e.g.
author.name
. Because the initial query withorder by
does not know whatauthor
column is:Solution
The proposed solution is to detect, if
order
references any columns in outside tables. If it does, modify the query tojoin
that outside table. In that case, queries withorder
will work as expected.Note: Similar work is done for
filter
in core/server/models/base/utils.js. Ifjoin
is required, it modifies a query to include these statements.The text was updated successfully, but these errors were encountered: