Jon*_*Jon 1 postgresql ruby-on-rails heroku
我只是尝试运行查询来查找数据库中的所有记录,这些记录的"datetime"列中的值小于当前的unix时间戳.
目前这是我的代码.它在当地运行良好.
t = Time.new.to_i
Event.where("datetime < #{t}")
Run Code Online (Sandbox Code Playgroud)
当我在heroku控制台中测试它时,我收到此错误.
>> Event.where("datetime < #{t}")
ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist: character varying < integer
LINE 1: SELECT "events".* FROM "events" WHERE (datetime < 132462148...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "events".* FROM "events" WHERE (datetime < 1324621488)
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1003:in `async_exec'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1003:in `exec_no_cache'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:591:in `block in exec_query'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您应该使用占位符来获取正确的格式并确保它被正确引用:
t = Time.new
events = Event.where("datetime < :t", :t => t)
Run Code Online (Sandbox Code Playgroud)
您无法timestamp在PostgreSQL中将列与整数进行比较,但您可以在SQLite中进行比较.您必须将您timestamp与另一个timestamp(或date)或可以解析为的字符串进行比较timestamp.这个SQL不起作用:
SELECT "events".* FROM "events" WHERE (datetime < 132462148)
Run Code Online (Sandbox Code Playgroud)
但这些将:
SELECT "events".* FROM "events" WHERE (datetime < '2011-12-23 06:52:25.096869')
SELECT "events".* FROM "events" WHERE (datetime < '2011-12-23')
Run Code Online (Sandbox Code Playgroud)
这里有几个教训: