使用SQLite在本地运行,但在Heroku上:PGError:ERROR:语法错误在“NULL”或接近“NULL”

时间:2011-02-06 02:42:47

标签: ruby-on-rails postgresql heroku

我的代码在本地sqllite运行正常,但当我推送到heroku时,我得到:

ActiveRecord::StatementInvalid (PGError: ERROR:  syntax error at or near "NULL"
LINE 1: ...LECT  "beeps".* FROM "beeps" WHERE ((widget_id NOT NULL) AND ...
                                                             ^
: SELECT  "beeps".* FROM "beeps" WHERE ((widget_id NOT NULL) AND (updated_at > '2011-02-06 02:27:59.867809') AND (userphone = '0000000xxxx') AND (beeper
= '0000000zzzz') AND (inbound = 't')) ORDER BY beeps.id DESC LIMIT 1):

ruby​​代码是:

Beep.last(:conditions => [ "(widget_id NOT NULL) AND (updated_at > ?) AND (userphone = ?) AND (beeper = ?) AND (inbound = ?)", 5.minutes.ago, aphone, beeper, inboundflag ])

顺便说一下,有没有办法使用纯活动记录来构造这个查询,所以我们可以(希望)避免使用特定于数据库的查询字符串?

1 个答案:

答案 0 :(得分:2)

你确定你在Heroku而不是MySQL上使用SQLLite吗?无论哪种方式,查询语法似乎有点不正确。我会将您的查询修改为:

Beep.last(:conditions => [ "(widget_id IS NOT NULL) AND (updated_at > ?) AND (userphone = ?) AND (beeper = ?) AND (inbound = ?)", 5.minutes.ago, aphone, beeper, inboundflag ])

如果您使用的是Rails3,您可以将这些条件链接在一起:

Beep.where('widget_id IS NOT NULL)
  .where('updated_at > ?', 5.minutes.ago)
  .where(userphone: aphone)
  .where(beeper: beeper)
  .where(inbound: inboundflag)
  .last