rails postgres date_time无法正常工作

时间:2015-03-17 00:16:54

标签: ruby-on-rails postgresql

尝试将日期时间保存到postgres时,我遇到了一个奇怪的错误。有时它有效,见第一个例子。其他时候它不起作用。

UPDATE "client_comments" SET "comment" = $1, "conversation_time" = $2, "updated_at" = $3 WHERE "client_comments"."id" = 2  [["comment", "test2"], ["conversation_time", "2015-03-09 23:42:00.000000"], ["updated_at", "2015-03-16 23:50:32.307101"]]

有效,没有问题!

当我使用带有

的表单时
<%= f.datetime_select :conversation_time, { :start_year => 2010, :end_year => Date.today.year } %>

我点击更新,参数是:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"iz/MS9mI3K7H0dg05AFjaiKTudLpuG3ipAtApGbHEQA=", "client_comment"=>{"client_id"=>"1221", "user_id"=>"1", "company_id"=>"MON", "conversation_time(1i)"=>"2015", "conversation_time(2i)"=>"3", "conversation_time(3i)"=>"7", "conversation_time(4i)"=>"19", "conversation_time(5i)"=>"42", "comment"=>"test2"}, "commit"=>"Update Client comment", "id"=>"2"}

sql是:

UPDATE "client_comments" SET "conversation_time" = $1, "updated_at" = $2 WHERE "client_comments"."id" = 2  [["conversation_time", "2015-03-08 00:42:00.000000"], ["updated_at", "2015-03-17 00:01:43.611166"]]

导致此异常:

PG::DatatypeMismatch: ERROR:  column "conversation_time" is of type timestamp without time zone but expression is of type time without time zone at character 52
HINT:  You will need to rewrite or cast the expression.

另一个奇怪的事情是这个案例中的小时是19,但是它试图在sql中将小时设置为00,所以我也不知道该怎么做。

这是我的架构:

# \d+ client_comments
                                                         Table       "public.client_comments"
  Column       |            Type             |                          Modifiers                           | Storage  | Stats target | Description 
  -------------------+-----------------------------+------------------------------     --------------------------------+----------+--------------+-------------
 id                | integer                     | not null default  nextval('client_comments_id_seq'::regclass) | plain    |              | 
 client_id         | integer                     |                                                              | plain    |              | 
 user_id           | integer                     |                                                              | plain    |              | 
 company_id        | integer                     |                                                              | plain    |              | 
 comment           | text                        |                                                              | extended |              | 
 employee_user_id  | integer                     |                                                              | plain    |              | 
 created_at        | timestamp without time zone |                                                              | plain    |              | 
 updated_at        | timestamp without time zone |                                                              | plain    |              | 
 conversation_time | timestamp without time zone |                                                              | plain    |              | 
 Indexes:
"client_comments_pkey" PRIMARY KEY, btree (id)
"index_client_comments_on_client_id" btree (client_id)
"index_client_comments_on_company_id" btree (company_id)
"index_client_comments_on_user_id" btree (user_id)
Has OIDs: no

rails v 4.1.8 psql(9.3.2)

1 个答案:

答案 0 :(得分:1)

嗯,这不是一个完美的答案,但事实证明这是一个导轨4.1和pg 0.17.1或它们一起工作的错误。我升级到rails 4.2.1(刚出来)和pg 0.18.1,一切都很好。注意pg 0.81.1和早期版本的rails有一个blob支持的bug,但4.2.1似乎修复了这个。