【问题标题】:heroku activerecord query errorsheroku activerecord 查询错误
【发布时间】:2012-08-16 19:09:33
【问题描述】:

我目前在 Heroku 上有一个应用程序,它不断地从这个 ActiveRecord 查询中抛出错误。

if category == "-1"
    where('category_id IS null')
else
    where('category_id IS ?', category)
end

'category_id IS null 的第一个查询工作正常,但第二个查询抛出这样的错误:

2012-08-16T18:58:03+00:00 app[web.1]:
2012-08-16T18:58:03+00:00 app[web.1]:
2012-08-16T18:58:03+00:00 app[web.1]: Started GET "/items?cpath=4" for 204.154.121.30            at 2012-08-16 18:58:03 +0000
2012-08-16T18:58:08+00:00 app[web.1]:
2012-08-16T18:58:08+00:00 app[web.1]: ActiveRecord::StatementInvalid (PGError: ERROR:  syntax error at or near "4"
2012-08-16T18:58:08+00:00 app[web.1]: LINE 1: SELECT COUNT(*) FROM "items"  WHERE (category_id IS 4)
2012-08-16T18:58:08+00:00 app[web.1]:                                                             ^
2012-08-16T18:58:08+00:00 app[web.1]:   app/controllers/items_controller.rb:30:in `index'
2012-08-16T18:58:08+00:00 app[web.1]: : SELECT COUNT(*) FROM "items"  WHERE (category_id IS 4)):
2012-08-16T18:58:08+00:00 app[web.1]:
2012-08-16T18:58:08+00:00 app[web.1]:
2012-08-16T18:58:08+00:00 app[web.1]: cache: [GET /items?cpath=4] miss
2012-08-16T18:58:08+00:00 app[web.1]:   Processing by ItemsController#index as HTML
2012-08-16T18:58:08+00:00 app[web.1]:   Parameters: {"cpath"=>"4"}
2012-08-16T18:58:08+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
2012-08-16T18:58:08+00:00 heroku[router]: GET yisifahan.herokuapp.com/items?cpath=4 dyno=web.1 queue=0 wait=0ms service=
5026ms status=500 bytes=728
2012-08-16T18:58:08+00:00 heroku[web.1]: Error R12 (Exit timeout) -> At least one process failed to exit within 10 seconds of SIGTERM
2012-08-16T18:58:08+00:00 heroku[web.1]: Stopping remaining processes with SIGKILL
2012-08-16T18:58:09+00:00 heroku[web.1]: Process exited with status 137

有谁知道如何解决这个问题?谢谢。

【问题讨论】:

    标签: sql ruby-on-rails ruby ruby-on-rails-3 postgresql


    【解决方案1】:

    我相信IS 比较运算符对于相等性无效。 (http://www.postgresql.org/docs/9.2/static/functions-comparison.html)

    所以你的 sn-p 应该是这样的:

    if category == "-1"
      where('category_id IS null')
    else
      where('category_id = ?', category)
    end
    

    老实说,您应该针对这些情况进行一些测试来检查您的代码。它不应该这样进入服务器。我还建议您在开发环境中使用 postgres 数据库,以便确保您的代码对其架构有效。

    【讨论】:

      【解决方案2】:

      使用where(category_id: category) 代替与数据库无关。

      您在开发环境中有 mysql 或 sqlite 对吗?

      【讨论】:

      • 如此,null 语句也一样where(category: nil)
      猜你喜欢
      • 2012-03-27
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多