通过 Rails 和 pg gem 插入 PostgreSQL 服务器时,“令牌“-”无效

jay*_*gee 5 ruby postgresql ruby-on-rails pg

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

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)
Run Code Online (Sandbox Code Playgroud)

这是我尝试插入到 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
    }
Run Code Online (Sandbox Code Playgroud)

这是我运行 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 ($1, $2, $3, $4, $5, $6, $7, $8, $9) 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 ($1, $2, $3, $4, $5, $6, $7, $8, $9) 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 ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"
Run Code Online (Sandbox Code Playgroud)

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

编辑:我在下面为 SalesEvent 添加了模型。我确实在那里有序列化 :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
Run Code Online (Sandbox Code Playgroud)

删除该特定行会返回相同的“令牌“-”无效”错误,尽管是由于不同的输入:

(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 ($1, $2, $3, $4, $5, $6, $7, $8, $9) 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 ($1, $2, $3, $4, $5, $6, $7, $8, $9) 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 ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"
Run Code Online (Sandbox Code Playgroud)

jay*_*gee 1

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