db-migrate / db-migrate/mysql

Question mark params not being replaced in runSql

Open
#19 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
51
Forks
37
PR merge metrics
No merged PRs in 30d

Description

I have some code like this:

```js
db.runSql('INSERT INTO authGroups (id, isActive, isLocked, name, description, created) VALUES(?, TRUE, TRUE, ?, ?, NOW())', [id, name, description])
```

I expect that the question marks in `sql` will be replaced with the values from `params` (as [the documentation](https://db-migrate.readthedocs.io/en/latest/API/SQL/) describes). But the sql is being run with the question marks still there. I'm getting this error:

```
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, TRUE, TRUE, ?, ?, NOW())'
```

These are the values of id, name, description:

```
id: '4dfad450-7c17-11e9-8808-0242ac170002'
name: 'Users'
description: 'Test.'
```

Btw, I'm using promises, not the callback function.

What's strange is if I add a callback function as the third parameter, the placeholders are replaced. This seems like a bug.

If I use this function instead of db.runSql, it works fine:

```js
const runSql = (db, query, params) => new Promise((resolve, reject) => {
db.runSql(query, params, (err, data) => {
if(err) reject(err)
else resolve(data)
})
})
```

For example:

```js
runSql(db, 'INSERT INTO authGroups (id, isActive, isLocked, name, description, created) VALUES(?, TRUE, TRUE, ?, ?, NOW())', [id, name, description])
```

---
Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/74527262-question-mark-params-not-being-replaced-in-runsql?utm_campaign=plugin&utm_content=tracker%2F12292998&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F12292998&utm_medium=issues&utm_source=github).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.