-
Notifications
You must be signed in to change notification settings - Fork 51
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
Sort instances query stably, if applicable to underlying DB implementation #114
Sort instances query stably, if applicable to underlying DB implementation #114
Conversation
Thanks for bringing this up. This is a valid problem and it needs a fix. There are multiple issues to consider though; Using SQLAlchemy, the user can already specify an order with order_by as a mapper arg. Perhaps it is possible to read that in if no other value is specified? Inspection of the model with every request is not the right approach. The sort clause should be cached in We could also reuse Finally, there is the underlying issue:
Your fix would address pagination without sort, but it does not yet address that issue when a user retrieves items in sorted order. The user might sort by a column that is not unique and then the sorted items would not be stable. A solution could be to have |
@lyschoening Agreed that this is a partial/WIP fix, and I like your proposed approach (I just wanted to get something up before leaving for the day yesterday). Let me take a look at: (1) modifying the Will push here later today hopefully (jamming a few things in before heading off on vacation tomorrow unfortunately). |
b1b7630
to
f585e2f
Compare
f585e2f
to
21c52db
Compare
@lyschoening I've updated my PR per your suggestions. LMK if this works for you (I can take a look at adding a test case for sort stability, but may not be able to get to that before taking off for a ~week). |
@boydgreenfield I'll have a look at it soon. It's evening here, so I won't be able to get back to you before you leave. Have a good vacation! |
I'm not 100% thrilled by the naming/implementation here (happy to take suggestions @lyschoening), butthe current Flask-Potion code can lead to odd bugs for databases that do not guarantee the return order for queries without anORDER BY
clause (e.g., Postgres).Specifically, we were seeing issues where we'd get duplicate records in the paginated results. The underlying issue is (depending on one's perspective) either a bug or (under/mis)documentation in Flask-SQLAlchemy's
paginate
method on the query.This PR fixes this for the
RelationalManager
by ordering by the primary key if no other order by clause is provided.Note that it may be necessary to also add the primary key to the order clause in the general
_query_order_by
call for Postgres. I'll try to investigate this more tomorrow Pacific time.