【问题标题】:Ruby on Rails timestamp is being converted into numeric by PostgreSQL on HerokuRuby on Rails 时间戳正在由 Heroku 上的 PostgreSQL 转换为数字
【发布时间】:2011-09-30 06:18:03
【问题描述】:

首先我会说有一个与我类似的问题:

heroku Postgres error - operator does not exist timestamp without timezone = integer

但这并没有解决我的问题。我正在查看的是 Heroku 上的工作人员中的此错误:

[Worker(host:026e9a98-87ae-4c0c-94c8-315843e68ac1 pid:1)] Buzz#digest! failed with ActiveRecord::StatementInvalid: PGError: ERROR:  operator does not exist: timestamp without time zone = numeric
LINE 1: ...FROM "buzz_topics" WHERE ("buzz_topics"."version" = 0.069980...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
SELECT * FROM "buzz_topics" WHERE ("buzz_topics"."version" = 0.0699802335812795) AND ("buzz_topics".buzz_id = 696)  LIMIT 1 - 0 failed attempts

问题是,Ruby on Rails 或 PostgreSQL 不知何故将时间戳转换成这个奇怪的小数。

它试图在 Ruby on Rails 中执行的查询看起来像这样:

BuzzTopic.first( :conditions => [ "version = ?", Feature.current_version ] )

BuzzTopic 有一个名为 current 的 named_scope,看起来与此类似。

Feature.current_version 只是一个时间戳:

>> Feature.current_version
=> Mon, 26 Sep 2011 07:06:41 UTC +00:00

BuzzTopic.version 也只是一个时间戳。

heroku run console 中使用该查询有效,但是当工作人员运行它时它不起作用。以下是我迄今为止尝试过的事情的清单:

我尝试将Feature.current_version 转换为类似“2011-09-26 07:06:41”的格式:

def self.current_version # original
  last_feature = enabled.last
  last_feature.present? ? last_feature.version : nil
end

def self.current_version
  last_feature = enabled.last
  last_feature.present? ? last_feature.version.utc.to_formatted_s( :db ) : nil
end

并为BuzzTopic 尝试了不同的命名范围:

named_scope :current, lambda { { :conditions => { :version => Feature.current_version } } } # original

named_scope :current, lambda { { :conditions => [ "version = to_timestamp( ? )", Feature.current_version ] } }

named_scope :current, lambda { { :conditions => [ "version = timestamp ?", Feature.current_version ] } }

这些都不起作用,我肯定尝试了一些我忘记的其他方法。我被困住了,不知道该怎么办,所以我来到了这里。有人可以帮忙吗?

【问题讨论】:

    标签: ruby-on-rails postgresql heroku timestamp


    【解决方案1】:

    实际上,问题是由于我在解决另一个问题时保留了一些代码。它没有在一个块中传递时间戳和小数,而是只传递了小数。现在我解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2016-08-23
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 2010-12-20
      • 1970-01-01
      • 2019-02-25
      相关资源
      最近更新 更多