Note that there are some explanatory texts on larger screens.

plurals
  1. POSqlite3 activerecord :order => "time DESC" doesn't sort
    primarykey
    data
    text
    <p>rails 2.3.4, sqlite3</p> <p>I'm trying this</p> <blockquote> <p>Production.find(:all, :conditions => ["time > ?", start_time.utc], :order => "time DESC", :limit => 100)</p> </blockquote> <p>The <em>condition</em> works perfectly, but I'm having problems with the <em>:order => time DESC</em>.</p> <p>By chance, I discovered that it worked at Heroku (testing with heroku console), which runs PostgreSQL. However, locally, using sqlite3, <strong>new entries</strong> will be sorted after old ones, no matter what I set <em>time</em> to. Like this (<em>output has been manually stripped</em>): second entry is new:</p> <blockquote> <p>Production id: 2053939460, time: "2010-04-24 23:00:04", created_at: "2010-04-24 23:00:05"</p> <p>Production id: 2053939532, time: "2010-04-25 10:00:00", created_at: "2010-04-27 05:58:30"</p> <p>Production id: 2053939461, time: "2010-04-25 00:00:04", created_at: "2010-04-25 00:00:04"</p> <p>Production id: 2053939463, time: "2010-04-25 01:00:04", created_at: "2010-04-25 01:00:04"</p> </blockquote> <p>Seems like it sorts on the primary key, <em>id</em>, not <em>time</em>. Note that the query works fine on heroku, returning a correctly ordered list! I like sqlite, it's so KISS, I hope you can help me...</p> <p>Any suggestions?</p> <hr> <p>UPDATE/SOLVED: <em>time</em> is a reserved sqlite3 keyword (<em>date</em>, amongst others, is too). This is why <code>:order =&gt; 'time DESC'</code> works in PostgreSQL (<a href="http://www.postgresql.org/docs/8.1/static/sql-keywords-appendix.html" rel="nofollow noreferrer"><em>non-reserved keyword</em></a>), but not in sqlite3. The solution is to avoid having sqlite3 keywords as column names if you ever intend to sort on them. <strong>Renaming solves the problem.</strong></p> <p>I've tested with the standard rails pattern <em>updated_at</em> and <em>created_at</em>, which works perfectly.</p> <p>I still prefer sqlite3 in development, it's so simple and smooth to work with, copy the database and send to your partner. Thanks to @newtover !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload