# ActiveRecord Postgres small inconsistency

**URL:** <https://discuss.rubyonrails.org/t/activerecord-postgres-small-inconsistency/63819>\
**Category:** rubyonrails-talk\
**Created:** [May 7, 2012, 7:31am UTC](https://discuss.rubyonrails.org/t/activerecord-postgres-small-inconsistency/63819 "2012-05-07T07:31:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahajiyev](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@rahajiyev](https://discuss.rubyonrails.org/u/rahajiyev)\
**Post date:** [May 7, 2012, 7:31am UTC](https://discuss.rubyonrails.org/t/activerecord-postgres-small-inconsistency/63819/1 "2012-05-07T07:31:51Z")

</div>

Hi, I'm using Rails 3.2.1 and following through Mike Hartl's tutorial.

irb(main):024:0\> User.first &nbsp;&nbsp;User Load (0.6ms) SELECT "users".\* FROM "users" LIMIT 1 =\> ... irb(main):025:0\> User.last &nbsp;&nbsp;User Load (1.0ms) SELECT "users".\* FROM "users" ORDER BY "users"."id" DESC LIMIT 1 =\> ...

Here's what PostgreSQL docs say concerning ORDER-less SELECTs: "If the ORDER BY clause is specified, the returned rows are sorted in the specified order. If ORDER BY is not given, the rows are returned in whatever order the system finds fastest to produce. (See ORDER BY Clause below.)"

So, shouldn't ORDER BY .. ASC be specified explicitly, instead of relying on an implementation detail that might later change?

Thanks.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Walker](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jeremy\_Walker](https://discuss.rubyonrails.org/u/Jeremy_Walker)\
**Post date:** [May 7, 2012, 4:34pm UTC](https://discuss.rubyonrails.org/t/activerecord-postgres-small-inconsistency/63819/2 "2012-05-07T16:34:49Z")

</div>

> Hi, I’m using Rails 3.2.1 and following through Mike Hartl’s
> 
> tutorial.
> 
> irb(main):024:0\> User.first
> 
> User Load (0.6ms) SELECT “users”.\* FROM “users” LIMIT 1
> 
> =\> …
> 
> irb(main):025:0\> User.last
> 
> User Load (1.0ms) SELECT “users”.\* FROM “users” ORDER BY
> 
> “users”.“id” DESC LIMIT 1
> 
> =\> …
> 
> Here’s what PostgreSQL docs say concerning ORDER-less SELECTs:
> 
> "If the ORDER BY clause is specified, the returned rows are sorted in
> 
> the specified order. If ORDER BY is not given, the rows are returned
> 
> in whatever order the system finds fastest to produce. (See ORDER BY
> 
> Clause below.)"
> 
> So, shouldn’t ORDER BY … ASC be specified explicitly, instead of
> 
> relying on an implementation detail that might later change?

This strikes me as a good suggestion. I’ll put a pull request together for it unless anyone has any objections.

Jeremy Walker

[http://www.ihid.co.uk](http://www.ihid.co.uk)

---

<div class="post-metadata">

**Author:** ![Jeremy\_Walker](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@Jeremy\_Walker](https://discuss.rubyonrails.org/u/Jeremy_Walker)\
**Post date:** [May 7, 2012, 4:52pm UTC](https://discuss.rubyonrails.org/t/activerecord-postgres-small-inconsistency/63819/3 "2012-05-07T16:52:57Z")

</div>

> > Hi, I’m using Rails 3.2.1 and following through Mike Hartl’s
> > 
> > tutorial.
> > 
> > irb(main):024:0\> User.first
> > 
> > User Load (0.6ms) SELECT “users”.\* FROM “users” LIMIT 1
> > 
> > =\> …
> > 
> > irb(main):025:0\> User.last
> > 
> > User Load (1.0ms) SELECT “users”.\* FROM “users” ORDER BY
> > 
> > “users”.“id” DESC LIMIT 1
> > 
> > =\> …
> > 
> > Here’s what PostgreSQL docs say concerning ORDER-less SELECTs:
> > 
> > "If the ORDER BY clause is specified, the returned rows are sorted in
> > 
> > the specified order. If ORDER BY is not given, the rows are returned
> > 
> > in whatever order the system finds fastest to produce. (See ORDER BY
> > 
> > Clause below.)"
> > 
> > So, shouldn’t ORDER BY … ASC be specified explicitly, instead of
> > 
> > relying on an implementation detail that might later change?

> This strikes me as a good suggestion. I’ll put a pull request together for it unless anyone has any objections.

After a quick browse through the code, this has already been added to Rails master and will appear in a future release.
