'Token "-" is invalid' 通过 Rails 和 pg gem 插入 PostgreSQL 服务器时

'Token "-" is invalid' when inserting into PostgreSQL server via Rails and pg gem

我最近开始尝试设置新的本地 PostgreSQL 服务器,连接到现有的 Rails 应用程序。我有一个 table 我想插入到:

postgres=# \d+ events.t_sales_events
                                                             Table "events.t_sales_events"
   Column   |            Type             |                              Modifiers                              | Storage  | Stats target | Description 
------------+-----------------------------+---------------------------------------------------------------------+----------+--------------+-------------
 id         | integer                     | not null default nextval('events.t_sales_events_id_seq'::regclass) | plain    |              | 
 event_name | character varying(45)       | not null                                                            | extended |              | 
 actor_id   | integer                     |                                                                     | plain    |              | 
 actor_type | character varying(45)       |                                                                     | extended |              | 
 json       | jsonb                       |                                                                     | extended |              | 
 created    | timestamp without time zone | not null                                                            | plain    |              | 
 modified   | timestamp without time zone | not null                                                            | plain    |              | 
 deleted    | timestamp without time zone |                                                                     | plain    |              | 
 timestamp  | timestamp without time zone | not null                                                            | plain    |              | 
 context    | text                        |                                                                     | extended |              | 
Indexes:
    "t_sales_events_pkey" PRIMARY KEY, btree (id)

这是我尝试插入 t_sales_events:

的事件示例
event =
    {
        :event_name => "lead_created",
           :context => nil,
        :actor_type => "sales_user",
         :timestamp => "2013-03-18T07:13:42.000+0000",
              :json => {
                       :dc_id => "00AA000000AAaaAAaa1",
                          :name => "John Doe",
                         :title => "CEO",
                          :role => nil,
                         :phone => nil,
                       :company => "Does Does",
                         :email => "jdoe@doesdoes.com",
                   :dc_source => nil,
                       :lead_id => nil
        },
          :actor_id => nil,
           :created => 2015-09-15 18:32:25 -0700,
          :modified => 2015-09-15 18:32:33 -0700
    }

这是我在 运行 SalesEvent.create(event):(0.2ms)

时得到的错误
BEGIN
  SQL (4.0ms)  INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES (, , , , , , , , ) RETURNING "id"  [["actor_id", nil], ["actor_type", "sales_user"], ["context", nil], ["created", Wed, 16 Sep 2015 01:32:25 UTC +00:00], ["deleted", nil], ["event_name", "lead_created"], ["json", "---\n:sfdc_id: 00AA000000AAaaAAaa1\n:name: John Doe\n:title: CEO\n:role: \n:phone: \n:company: Does Does\n:email: jdoe@doesdoes.com\n:lead_id: \n"], ["modified", Wed, 16 Sep 2015 01:32:33 UTC +00:00], ["timestamp", Mon, 18 Mar 2013 07:13:42 UTC +00:00]]
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type json
DETAIL:  Token "-" is invalid.
CONTEXT:  JSON data, line 1: -...
: INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES (, , , , , , , , ) RETURNING "id"
   (0.1ms)  ROLLBACK
*** ActiveRecord::StatementInvalid Exception: PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type json
DETAIL:  Token "-" is invalid.
CONTEXT:  JSON data, line 1: -...
: INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES (, , , , , , , , ) RETURNING "id"

在我看来,错误的出现是因为“---”被添加到标记为 'json' 的字段的开头,但我不知道为什么。

编辑: 我在下面添加了 SalesEvent 模型。我确实在那里有 serialize :json 。抱歉 - 我忘记了我在某种程度上盲目尝试尝试解决相同的错误时添加了这一点。

class SalesEvent < ActiveRecord::Base

  establish_connection :warehouse_development
  self.table_name = "events.t_sales_events"

  serialize :json

  def self.get_all
    SalesEvent.all
  end

end

删除特定行 returns 同样的 'Token "-" is invalid' 错误,尽管是由于不同的输入:

(0.1ms)  BEGIN
  SQL (4.2ms)  INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES (, , , , , , , , ) RETURNING "id"  [["actor_id", nil], ["actor_type", "sales_user"], ["context", nil], ["created", Wed, 16 Sep 2015 06:09:00 UTC +00:00], ["deleted", nil], ["event_name", "lead_created"], ["json", {:sfdc_id=>"00AA000000AAaaAAaa1", :name=>"John Doe", :title=>"CEO", :role=>nil, :phone=>nil, :company=>"Does Does", :email=>"jdoe@doesdoes.com", :dc_source=>nil, :lead_id=>nil}], ["modified", Wed, 16 Sep 2015 06:09:07 UTC +00:00], ["timestamp", Mon, 18 Mar 2013 07:13:42 UTC +00:00]]
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type json
DETAIL:  Token "-" is invalid.
CONTEXT:  JSON data, line 1: -...
: INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES (, , , , , , , , ) RETURNING "id"
   (0.1ms)  ROLLBACK
*** ActiveRecord::StatementInvalid Exception: PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type json
DETAIL:  Token "-" is invalid.
CONTEXT:  JSON data, line 1: -...
: INSERT INTO "events"."t_sales_events" ("actor_id", "actor_type", "context", "created", "deleted", "event_name", "json", "modified", "timestamp") VALUES (, , , , , , , , ) RETURNING "id"

原来是 rails 3.2.17 中的 postgres 连接器与 jsonb 对象不兼容。升级到 4.2.4 后,错误不再出现。