Rails with Postgres data is returned out of order

自古美人都是妖i 提交于 2019-12-21 18:33:47

问题


I've just transitioned my app from MySQL to Postgres. Previously, a request for .all returned all the rows in id order. On Postgres the rows are returned out of order. Likewise,

Person.first

used to return the record with id 1, now it sometimes returns another record.

If I add an order clause like this:

Person.order("id").first

The query succeeds and returns the first row. Is this expected behaviour?


回答1:


this post answers your question:

I don't think ordering by ID is guaranteed by default, since I believe it's up to the database how a non-ordered query gets returned. You can make it be ordered so by defining a default scope at the top of your model like so:

default_scope order('id ASC')

Then calling Model.all will be equivalent to calling Model.order('id ASC').



来源:https://stackoverflow.com/questions/14755079/rails-with-postgres-data-is-returned-out-of-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!