10

I want to insert a row with JSONB content as a raw query. The problem is that I get error wherever I insert any "nontrivial" JSON object.

These are examples of my raw SQL queries:

Error for all of these: SequelizeBaseError: invalid input syntax for type json

'INSERT INTO "sometable" ("id", "createdAt", "updatedAt", "custom_data") VALUES (DEFAULT, NOW(), NOW(), \'"{something:\"value\"}"\')';
'INSERT INTO "sometable" ("id", "createdAt", "updatedAt", "custom_data") VALUES (DEFAULT, NOW(), NOW(), \'"{\"aaa\":\"ss15\"}"\')';

This last one works, but only with integer values:

'INSERT INTO "sometable" ("id", "createdAt", "updatedAt", "custom_data") VALUES (DEFAULT, NOW(), NOW(), \'"{a:10}"\')';

I've tried so many possible combinations of escaping the JSON-like string, even inserting real JS object, and nothing worked.

1 Answer 1

13

Try to JSON.stringify() first:

var customData = JSON.stringify({"name":"George Washington","presidentNo":1});
Sign up to request clarification or add additional context in comments.

1 Comment

Confirmed on Sequelize V6 as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.