Inserting JSON Array produces error
See original GitHub issueHi all,
I’ve already looked at #94 and #864 and still cannot get this to work. I’ve also tried @ricardogama’s bookshelf-json-columns plugin with no luck.
As other’s have described, the issue is that an error is thrown when inserting a JSON Array into a JSONB column in Postgres. Inserting a JSON object works fine, just not an array of JSON objects. There is a known issue that you must stringify the input before passing it through node-pg – this is what the bookshelf-json-columns plugin does. Unfortunately, when an array of these objects is passed, it appears that Bookshelf is stringifying the input multiple times. Here’s an error when trying to insert a JSON array into a JSONB column:
[
{ serviceId: 2, petId: 5, checked: true },
{ serviceId: 3, petId: null, checked: false }
]
After stringifying:
"[{"serviceId":2,"petId":5,"checked":true},{"serviceId":3,"petId":null,"checked":false}]"
Insert error:
{"msec":54.03032702207565,
"error":"update \"appointment\" set \"ended_at\" = $1, \"report_card\" = $2, \"staff_notes\" = $3, \"status\" = $4, \"updated_at\" = $5 where \"id\" = $6 - invalid input syntax for type json","data":{"message":"update \"appointment\" set \"ended_at\" = $1, \"report_card\" = $2, \"staff_notes\" = $3, \"status\" = $4, \"updated_at\" = $5 where \"id\" = $6 - invalid input syntax for type json",
"severity":"ERROR",
"code":"22P02",
"condition":"invalid_text_representation",
"detail":"Expected \":\", but found \",\".","where":"JSON data, line 1: {\"{\\\"serviceId\\\":2,\\\"petId\\\":5,\\\"checked\\\":true}\",...","file":"json.c","line":1198,"routine":"report_parse_error","name":"PgError","isBoom":true,"isServer":true,"data":null,"output":{"statusCode":500,"payload":{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"},"headers":{}}}}
You’ll notice that the output detailed in the error message contains a number of extra slashes, which appear to be caused by stringifying the input multiple times.
However, if I do the following in Knex, I get no errors, which leads me to believe this is a Bookshelf issue:
return knex.table('appointment').insert({
business_id: businessId,
staff_id: staffId,
customer_id: customerId,
report_card: JSON.stringify([
{ serviceName: 'Poop', 'petName': 'JJ', checked: true },
{ serviceName: 'Pee', 'petName': 'Mario', checked: true },
{ serviceName: 'Poop', 'petName': 'JJ', checked: true },
{ serviceName: 'Pee', 'petName': 'Mario', checked: true }
])
});
I started digging through the Bookshelf code to find out where this is happening, but someone who knows the codebase better may be able to expedite things.
Thanks, James
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:9 (2 by maintainers)
@cjnqt use the plugin I mentioned above. @ricardogama just released a new version that resolves this issue.
I got similar issue similar issue with following error
code: '22P02', detail: 'Expected ":", but found "}".', hint: undefined, position: undefined, internalPosition: undefined, internalQuery: undefined, where: 'JSON data, line 1: {"{\\"l\\":\\"b\\"}"}',
& using plugin bookshelf-json-columnsissue was model had initialize method
after adding
Bookshelf.Model.prototype.initialize.apply(this, arguments);
ininitialize
method solved the issue