简体   繁体   English

活动记录查询和heroku问题。

[英]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: 这里有几节课:

  1. You should also start developing on top of PostgreSQL if you're going to be deploying to Heroku, ActiveRecord will not insulate you from all of the differences between various databases. 如果要部署到Heroku,还应该开始在PostgreSQL上进行开发,ActiveRecord不会使您摆脱各种数据库之间的所有差异。
  2. You should let ActiveRecord worry about the type conversion issues as much as possible, if you're comparing against a date or a time, use a placeholder and hand AR a time object of some sort and let AR worry about it. 您应该让ActiveRecord尽可能多地担心类型转换问题,如果要与日期或时间进行比较,请使用占位符并将AR传递给某种时间对象,然后让AR对其进行担心。
  3. Use placeholders instead of string interpolation wherever possible. 尽可能使用占位符而不是字符串插值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM