-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raw query replacements in MySQL INSERT query #9050
Comments
Have you tried db.query('INSERT INTO product (a, b) VALUES (?), (?);', {
replacements: [['a', 'b'], ['c', 'd']],
type: Sequelize.QueryTypes.INSERT
}); Might be possible, but not sure |
@sushantdhiman yes, it will work. But how to do that if I don't know number of items? |
Well you can repeat, db.query(
`INSERT INTO product (a, b) VALUES ${data.map(a => '(?)').join(',')};`,
{
replacements: data,
type: Sequelize.QueryTypes.INSERT
}
); |
@sushantdhiman, but most of MySQL drivers can do it from the box. I probably need to describe my case. In my project we are doing a migration from plain queries to ORM. As a first step we decide to use sequelize library to perform all SQL requests. And it was bad news for me that it's not compatible with usual placeholder syntax (like here). |
We are using |
Can you test if |
@sushantdhiman, in const mysql = require('mysql2');
const connection = mysql.createConnection({host: 'localhost', user: 'root', database: 'test', debug: true});
connection.query(
'INSERT INTO product (a, b) VALUES ?;',
[[['a', 'b'], ['c', 'd']]],
(...args) => console.log(args)
); In debug log: |
@sushantdhiman, can you please review the pull request I did. It's almost copy from SqlString library, which is used in |
Thank you! |
What do you expect to happen?
What is actually happening?
Dialect: mysql
Sequelize version: 4.32.3
So, the question is, how to insert non-determined number of items in one query?
The text was updated successfully, but these errors were encountered: