Heroku 上的 PostgreSQL 正在将 Ruby on Rails 时间戳转换为数字
我首先要说有一个与我类似的问题:
heroku Postgres错误 - 运算符不存在没有时区=整数的时间戳
,但它没有解决我的问题。我看到的是 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 的 name_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
并为 尝试了不同的named_scopes 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 ] } }
这些都不起作用,而且我确实尝试了一些我忘记的其他方法。我被困住了,不知道该怎么办,所以我来到了这里。有人可以帮忙吗?
I will start out by saying there is a question similar to mine:
heroku Postgres error - operator does not exist timestamp without timezone = integer
but it did not solve my problem. What I am looking at is this error in the worker on 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
The problem is, somehow Ruby on Rails or PostgreSQL converted a timestamp into this weird decimal.
The query it is trying to do in Ruby on Rails looks something like this:
BuzzTopic.first( :conditions => [ "version = ?", Feature.current_version ] )
BuzzTopic
has a named_scope called current that looks similar to this.
Feature.current_version
is just a timestamp:
>> Feature.current_version
=> Mon, 26 Sep 2011 07:06:41 UTC +00:00
BuzzTopic.version
is also just a timestamp.
Using that query in heroku run console
works, however it does not work when the worker runs it. Here is a list of things I have tried so far:
I tried converting Feature.current_version
into a format like "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
and tried different named_scopes for 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 ] } }
None of these worked, and I definitely tried some others that I forgot about. I am stuck and don't know what to do, so I came here. Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,这个问题是由于我在解决另一个问题时保留了一些代码而导致的。它没有在块中传递时间戳和小数点,而是只传递小数点。现在我解决了这个问题,这个问题就解决了。
Actually, the problem was due to some code that I left unchanged when I was solving another problem. Instead of passing a timestamp and a decimal in a block, it only passed the decimal. Now that I fixed that, this problem is solved.