-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
The current order by id recommendation may be subtly introducing bugs. #274
Comments
Af far as I'm aware, More changes on the front of better handling of precision in Rails: [1, 2]. The case when timestamps would have identical microseconds are quite rare. Can't think of a case when one would want to use pagination to navigate through (quite lengthy supposedly) list of such models. The current wording may be confusing as it contains two recommendations:
Those two should not necessarily be followed together, and the latter is more an example of what the Would you like to send a pull request to clarify this, @kitsunde? Additionally, in some cases, e.g. for tree-structured comments where the primary sorting key may have gaps, sorting by such and |
I used a simple date for the sake of simplicity. But here is a fairly common concrete pattern: now = Time.current
Book.insert_all([
{ title: "Rework", author: "David", created_at: now },
{ title: "Eloquent Ruby", author: "Russ", created_at: now }
]) Any batch operation pointing to a single timestamp, any operation that uses I'm happy to make a PR. |
Again, I see what you mean, but again, for those bulk insert operations ordering by a tuple of I'd like to reiterate that the first part of the guideline, not sorting by |
But yet that would be the closest to the real chronological order. I think that the juice is that chronological is highly dependent on business logic and implementation details, so probably the second part of the message should reinforce that idea and not only suggest the order by created_at. |
Point is that the chronological order is just one of the possible use cases. Any other column combination might be a better option can be more desirable depending on business requirements. If we are talking of something that is not a blog for 15 mins. Chronological order is just one example of what should be used instead of ordering by |
The current wording
suggests ordering by
I would disagree. Rails adds timestamps by default when the migration to create a table is generated.
It would be strange to expect those records to have any meaningful chronology for records created in a batch in such a way. I agree with you that the current wording is misleading because it suggests using a timestamp column as the default ordering key and has no mention that it should be only used when the business logic requires chronological order. |
I also find this recommendation to be problematic. As mentioned above, it's common for records to have the same I think this recommendation should be dropped from the guide, as it's very specific to what you're doing on the page. For pagination in particular, it's important that your ordering includes a unique column. We've added Ordering by |
@mockdeep Sounds reasonable. What makes Do you think
is incorrect? In the Rails world, using Can't find any other justifications for this guideline in its current form off the top of my head. Side note: "order by |
@pirj I think it's maybe a little misleading. It is guaranteed to be in the order of the ids. The caveat I think it's trying to get at is that it might not be strictly chronological, but for the vast majority of Rails apps it is probably equivalent unless they're using UUIDs. I can't think of a good general principle for this. There could maybe be a couple of guidelines, but I can't imagine how it could possibly be linted via RuboCop. Basically, it comes down to "be aware of the implications of how you order records in your application". But those implications can vary from application to application, and page to page in an application. Many of our tables allow sorting on arbitrary columns, and each of them should have a stable tie breaker when the sort property is not unique. Sorting by |
I would like to suggest removing https://rails.rubystyle.guide/#order-by-id as a recommendation, or at least rewording it under a chronological ordering headline and changing the recommendation so it's not causing unstable sorting issues.
The background is:
id
(assumed to be an auto-incrementing sequence) will grant each row 1 unique id.ORDER BY
is not explicitly dictating order.created_at
and such timestamp fields are not (commonly) unique fields.This means that:
With
ORDER BY created_at
will get returned both as1,2
and as2,1
and this is the expected behaviour. These issues are quite common to find when doing pagination and finding the same record across multiple pages, but even repeatedly asking for the same records will cause records to swap places.I think this style guide is mixing up presentation layer concerns like showing an order that makes sense to an end-user on a specific column (in this case created time, but it could be a title, updated time or any other non-unique column) - which is situational - with general application-level concerns.
If I wanted to really nitpick the recommendation then timestamps like
created_at
are not really more reliable than the sequence itself in terms of the chronological order of records since they are in rails commonly set by application servers where clocks have drifted, db calls arrive and execute at different speeds and native SQL likeNOW()
is frozen to the first invocation inside of a transaction across the duration of the active transaction (in pg anyways) which can take an arbitrarily long time. If anything the sequence is probably closer to the actual chronological order of the record being inserted.If it's important to keep this recommendation then the id should probably be kept in the order call so queries remain deterministic I.e:
The text was updated successfully, but these errors were encountered: