Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert from select in sequelize

We use sequelize on node js I have resourse File. File has attribute UserId. Need: copy same files (by filesId) to user. If use sql then need do:

INSERT INTO files (name, link, userId) SELECT name, link, 5 FROM files WHERE id in (1,2,3,4,10)

But how do it on sequelize I not have any ideas... Only custom sql query.

like image 736
Stefan Korf Avatar asked May 15 '15 13:05

Stefan Korf


1 Answers

You can use sequelize.query() to run raw queries.

await sequelize
.query(
    `INSERT INTO files (name, link, userId) 
     SELECT name, link, 5 FROM files WHERE id in (1,2,3,4,10)`
)
.then((r) => r)
.catch((err) => {
    throw err;
});
like image 73
Ashok Kumar Avatar answered Oct 19 '22 15:10

Ashok Kumar