[英]Issue with active record querying and heroku.
I'm simply trying to run a query to find all the records in a database that have a value in a "datetime" column that's less than the current unix timestamp. 我只是试图运行查询以查找数据库中“ datetime”列中的值小于当前unix时间戳的所有记录。
Currently this is my code. 目前这是我的代码。 It runs fine locally.
它在本地运行良好。
t = Time.new.to_i
Event.where("datetime < #{t}")
When I test this in the heroku console I get this error. 当我在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'
Any ideas? 有任何想法吗?
You should be using a placeholder to get the right format and ensure that it is properly quoted: 您应该使用占位符以获取正确的格式并确保正确引用该格式:
t = Time.new
events = Event.where("datetime < :t", :t => t)
You can't compare a timestamp
column with an integer in PostgreSQL but you can in SQLite. 您无法在PostgreSQL中将
timestamp
列与整数进行比较,但可以在SQLite中进行比较。 You have to compare your timestamp
with another timestamp
(or date
) or a string that can be parsed as a timestamp
. 您必须将您的
timestamp
与另一个timestamp
(或date
)或可以被解析为timestamp
的字符串进行比较。 This SQL won't work: 此SQL无法使用:
SELECT "events".* FROM "events" WHERE (datetime < 132462148)
but these will: 但是这些将:
SELECT "events".* FROM "events" WHERE (datetime < '2011-12-23 06:52:25.096869')
SELECT "events".* FROM "events" WHERE (datetime < '2011-12-23')
There are several lessons here: 这里有几节课:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.