简体   繁体   中英

How to get last insert id of a record from query builder

This is what i am doing to insert record in mysql database by using query-builder .

//Add Schedule
exports.addSchedule = function addSchedule(data){ 

    return new Promise(function(resolve, reject){

    db.queryBuilder.insert(tableName, data, (err, res) => {
        if(res){
            console.log(db.queryBuilder.);
            resolve(true);

        }else{
            reject(false);
        }

        console.log(db.queryBuilder.last_query());
    });
 });
}

After insertion i need the insert-id auto generated key that mysql insert created for that record.

I need insert-id add record in another table.

Variable res contains property insert_id , see documentation :

qb.insert('articles', data, (err, res) => {
  qb.release();
  if (err) return console.error(err);

  const page_data = {
    article_id: res.insert_id,
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM