【发布时间】:2015-12-12 10:51:53
【问题描述】:
我最近开始尝试设置一个新的本地 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)
这是我尝试插入 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 ($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"
在我看来,错误的出现是因为“---”被添加到标记为“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
删除该特定行会返回相同的“令牌”-“无效”错误,尽管是由于不同的输入:
(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"
【问题讨论】:
-
看起来 json 字段传递的是 YAML 值,而不是 JSON。
-
你的
Event模型是什么样的? @CraigRinger 的正确观察表明您的模型中有serialize :json。 -
@CraigRinger 和 @muistooshort 我已按照建议添加到模型中。我的模型中确实有
serialize :json。我想我最初是为了解决同样的问题而添加的。不幸的是,删除似乎会引发同样的错误。 -
你用的是什么版本的rails?
-
@fabio Rails 3.2.17 EDIT 认为我明白了问题所在 - 根据guides.rubyonrails.org/4_2_release_notes.html,rails 似乎不支持 jsonb 类型连接器直到导轨 4.2。我将升级到 4.2,看看是否能解决问题。
标签: ruby-on-rails ruby postgresql pg