Sqlite3 activerecord:顺序=> “时间DESC”不排序

发布于 2024-08-30 05:48:46 字数 1398 浏览 12 评论 0原文

Rails 2.3.4,sqlite3

我正在尝试这个

Production.find(:all, :conditions => ["时间 > ?", start_time.utc], :order => “时间降序”, :限制=> 100)

条件工作完美,但我在 :order => 方面遇到问题时间 DESC

一次偶然的机会,我发现它可以在运行 PostgreSQL 的 Heroku 上运行(使用 heroku 控制台进行测试)。然而,在本地,使用 sqlite3,新条目将在旧条目之后排序,无论我设置什么时间。像这样(输出已被手动删除):第二个条目是新的:

生产 ID:2053939460,时间:“2010-04-24 23:00:04”,created_at:“2010-04-24 23:00:05”

生产 ID:2053939532,时间:“2010-04-25 10:00:00”,created_at:“2010-04-27 05:58:30”

生产 ID:2053939461,时间:“2010-04-25 00:00:04”,created_at:“2010-04-25 00:00:04”

生产 ID:2053939463,时间:“2010-04-25 01:00:04”,created_at:“2010-04-25 01:00:04”

看起来它是按主键排序的,id< /em>,而不是时间。请注意,查询在 Heroku 上运行良好,返回正确排序的列表!我喜欢 sqlite,它是如此的 KISS,我希望你能帮助我......

有什么建议吗?


更新/解决: time 是保留的 sqlite3 关键字(date 等也是保留的)。这就是为什么 :order => 'time DESC' 适用于 PostgreSQL (非保留关键字),但在sqlite3中没有。解决方案是如果您打算对它们进行排序,请避免使用 sqlite3 关键字作为列名称。 重命名可以解决问题。

我已经使用标准 Rails 模式 updated_atcreated_at 进行了测试,效果非常好。

在开发中我还是更喜欢sqlite3,它使用起来非常简单和流畅,复制数据库并发送给你的合作伙伴。感谢@newtover!

rails 2.3.4, sqlite3

I'm trying this

Production.find(:all, :conditions => ["time > ?",
start_time.utc], :order => "time DESC",
:limit => 100)

The condition works perfectly, but I'm having problems with the :order => time DESC.

By chance, I discovered that it worked at Heroku (testing with heroku console), which runs PostgreSQL. However, locally, using sqlite3, new entries will be sorted after old ones, no matter what I set time to. Like this (output has been manually stripped): second entry is new:

Production id: 2053939460, time: "2010-04-24 23:00:04", created_at: "2010-04-24 23:00:05"

Production id: 2053939532, time: "2010-04-25 10:00:00", created_at: "2010-04-27 05:58:30"

Production id: 2053939461, time: "2010-04-25 00:00:04", created_at: "2010-04-25 00:00:04"

Production id: 2053939463, time: "2010-04-25 01:00:04", created_at: "2010-04-25 01:00:04"

Seems like it sorts on the primary key, id, not time. 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...

Any suggestions?


UPDATE/SOLVED:
time is a reserved sqlite3 keyword (date, amongst others, is too). This is why :order => 'time DESC' works in PostgreSQL (non-reserved keyword), but not in sqlite3. The solution is to avoid having sqlite3 keywords as column names if you ever intend to sort on them. Renaming solves the problem.

I've tested with the standard rails pattern updated_at and created_at, which works perfectly.

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2024-09-06 05:48:46

使用不带引号的保留字通常是个坏主意。 time 是 SQLite 中的内置函数,请尝试使用以下函数来代替,并更好地消除歧义:

Production.find(:all,
                :conditions => ["`time` > ?", start_time.utc],
                :order => "`time` DESC",
                :limit => 100)

UPD:问题似乎出现在SO:

Rails Active Record find(:all, :order => ) 问题

It is usually a bad idea to use reserved words without surrounding quotes. time is a built-in function in SQLite, try using the following instead and better get rid of the ambiguity in the first place:

Production.find(:all,
                :conditions => ["`time` > ?", start_time.utc],
                :order => "`time` DESC",
                :limit => 100)

UPD: The problem seems to have appeared on SO:

Rails Active Record find(:all, :order => ) issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文