mariano/node-db-mysql — Issue #54: last_inserted_id is interfering on async batch insert queries
Author: threedot
Created: 2012-02-06 20:28:51+00:00
State: open

**res.id** is unstable when execute query asyncly

**Unstable Sample:**

```javascript
var list = [
{name:"foo", childs:['nick', 'jack','joe']},
{name:"bar", childs:['kate', 'kiti', 'kylie']},
{name:"baz", childs: ['ali', 'veli', 'deli']}
];

list.forEach(function(item){
db.query().insert('names', ['name'],[item.name]).execute(function(err, res){
if (err) throw new Error(err);
var nameId = res.id; // res.id's value is sometimes zero or others' inserts' ids
console.log(item.name, ' is inserted successfully');
item.childs.forEach(function(child){
db.query().insert('childs_of_names',['name_id', 'child'], [nameId, child]).execute(function(err, res){
if (err) throw new Error(err);
console.log(child, ' of ', item.name, ' is inserted successfully');
console.log('last inserted id of ', item.name, ' is ', res.id); // res.id's value is sometimes zero or others' inserts' ids
});
});
});
});
```

**Stable Sample but it isn't async:**

```javascript
var list = [
{name:"foo", childs:['nick', 'jack','joe']},
{name:"bar", childs:['kate', 'kiti', 'kylie']},
{name:"baz", childs: ['ali', 'veli', 'deli']}
];

list.forEach(function(item){
db.query().insert('names', ['name'],[item.name]).execute(function(err, res){
if (err) throw new Error(err);
var nameId = res.id; // res.id's value is sometimes zero or others' inserts' ids
console.log(item.name, ' is inserted successfully');
item.childs.forEach(function(child){
db.query().insert('childs_of_names',['name_id', 'child'], [nameId, child]).execute(function(err, res){
if (err) throw new Error(err);
console.log(child, ' of ', item.name, ' is inserted successfully');
console.log('last inserted id of ', item.name, ' is ', res.id); // res.id's value is sometimes zero or others' inserts' ids
}, {async:false});
});
}, {async:false});
});
```