0

I want to save some data to postgres database using sequelize but getting syntax error. One of my column name is 'from'.

let sql = await db.sequelize.query(INSERT INTO chats ( from, msg, chat_room_id) VALUES (${sender},'${msg}',${chat_room_id}));

The syntax error is caused by 'from' column as syntax error at or near "from"

I tried this

let sql = await db.sequelize.query(INSERT INTO chats ( from, msg, chat_room_id) VALUES (${sender},'${msg}',${chat_room_id}));

Expected: Data to be saved into database What I got: syntax error at or near "from"

1 Answer 1

2

If you use reserved PostgreSQL keywords as column names (I don't recommend to do so) then you need to wrap them into "":

let sql = await db.sequelize.query(INSERT INTO chats ( "from", msg, chat_room_id) VALUES (${sender},'${msg}',${chat_room_id}));

The same goes for column names with special characters or with mixed case like chatRoomId.

Sign up to request clarification or add additional context in comments.

Comments

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.