ActiveRecord插入返回PG :: DatetimeFieldOverflow错误

时间:2016-06-29 09:50:26

标签: ruby-on-rails activerecord

我已经搜索了SO,我找到了关于错误的答案,但我的问题却没有什么不同。我有这个错误:

PG::DatetimeFieldOverflow: ERROR:  date/time field value out of range: "24/05/2016 17:00"
HINT:  Perhaps you need a different "datestyle" setting.

如果我查看日志

    pry(#<WebexSync::Sync>)> attributes
    => {:webex_external_id=>1025, :user_id=>2565, :lesson_times=>[2016-05-24 17:00:00 +0200], :status=>"invited"}

pry(#<WebexSync::Sync>)> UserWebex.create! attributes
  User Load (1.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2565]]
  Webex Load (0.6ms)  SELECT  "webexes".* FROM "webexes" WHERE "webexes"."external_id" = $1  ORDER BY "webexes"."start_date" DESC LIMIT 1  [["external_id", 1025]]
  UserWebex Exists (0.5ms)  SELECT  1 AS one FROM "user_webexes" WHERE ("user_webexes"."user_id" = 2565 AND "user_webexes"."webex_external_id" = 1025) LIMIT 1
  SQL (2.3ms)  INSERT INTO "user_webexes" ("webex_external_id", "user_id", "lesson_times", "status") VALUES ($1, $2, $3, $4) RETURNING "id"  [["webex_external_id", 1025], ["user_id", 2565], ["lesson_times", "{24/05/2016 17:00}"], ["status", "invited"]]
PG::DatetimeFieldOverflow: ERROR:  date/time field value out of range: "24/05/2016 17:00"
HINT:  Perhaps you need a different "datestyle" setting.
: INSERT INTO "user_webexes" ("webex_external_id", "user_id", "lesson_times", "status") VALUES ($1, $2, $3, $4) RETURNING "id"
ActiveRecord::StatementInvalid: PG::DatetimeFieldOverflow: ERROR:  date/time field value out of range: "24/05/2016 17:00"
HINT:  Perhaps you need a different "datestyle" setting.

我无法理解为什么我的对象参数是正常的,并且日期采用此格式[2016-05-24 17:00:00 +0200],但保存ActiveRecord时更改了["lesson_times", "{24/05/2016 17:00}"]

中的格式

lesson_times是一个数组

1 个答案:

答案 0 :(得分:0)

正如错误所说:HINT: Perhaps you need a different "datestyle" setting. ,这是真的。

以下是如何在PG中设置不同的datestyle

SET datestyle = "ISO, DMY";