Skip to content
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

Closed
Kirill89 opened this issue Feb 14, 2018 · 9 comments
Closed

Raw query replacements in MySQL INSERT query #9050

Kirill89 opened this issue Feb 14, 2018 · 9 comments
Labels
type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@Kirill89
Copy link
Contributor

Kirill89 commented Feb 14, 2018

db.query('INSERT INTO product (a, b) VALUES ?;', {
        replacements: [[['a', 'b'], ['c', 'd']]],
        type: Sequelize.QueryTypes.INSERT
});

What do you expect to happen?

INSERT INTO product (a, b) VALUES ('a', 'b'), ('c', 'd');

What is actually happening?

INSERT INTO product (a, b) VALUES 'a','b','c','d';

Dialect: mysql
Sequelize version: 4.32.3

So, the question is, how to insert non-determined number of items in one query?

@sushantdhiman
Copy link
Contributor

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

@Kirill89
Copy link
Contributor Author

@sushantdhiman yes, it will work. But how to do that if I don't know number of items?

@sushantdhiman
Copy link
Contributor

Well you can repeat, (?) to length of your array

db.query(
  `INSERT INTO product (a, b) VALUES ${data.map(a => '(?)').join(',')};`,
  {
     replacements: data,
     type: Sequelize.QueryTypes.INSERT
  }
);

@Kirill89
Copy link
Contributor Author

@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).

@sushantdhiman
Copy link
Contributor

We are using mysql2, I dont think it support array replacement

@sushantdhiman
Copy link
Contributor

Can you test if mysql2 can handle that sort of replacement, if it can I am willing to tag this as a feature request

@Kirill89
Copy link
Contributor Author

@sushantdhiman, in mysql2 it works.

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: Sending query command: INSERT INTO product (a, b) VALUES ('a', 'b'), ('c', 'd');

@sushantdhiman sushantdhiman added the type: feature For issues and PRs. For new features. Never breaking changes. label Feb 15, 2018
@Kirill89
Copy link
Contributor Author

@sushantdhiman, can you please review the pull request I did. It's almost copy from SqlString library, which is used in mysql2. If it's looks suitable for you, I will add tests and update documentation.

@Kirill89
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

No branches or pull requests

2 participants