Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodjs sequalize how to get last insert id

I am using Sequalize with nodejs to insert a row in a table,now i want that last inserted id,i want some thing like "mysql_insert_id()", here is my code:

function registerAgent(agent_data,request_key,res,next)
{
  models.agent.create({creator_id: agent_data.userid },  { fields: [ 'creator_id'] }).then(function(user) {
      console.log(user);
    }); 
};
like image 522
Muzammil Naseer Avatar asked May 31 '16 11:05

Muzammil Naseer


People also ask

How do I get the last inserted row ID?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL.

How do you get all records by Created date today in Sequelize?

const TODAY = new Date(); const SUM = await OrdersModel. sum('price', { where: { created: Sequelize. DATE(TODAY), }, }); console. log(SUM);


1 Answers

The create() return the last insert element, so the last id is user.id. Is this for your problem?

function registerAgent(agent_data,request_key,res,next)
{
  models.agent.create({creator_id: agent_data.userid },  { fields: [ 'creator_id'] }).then(function(user) {
      console.log(user.id);
    }); 
};
like image 193
WeiYuan Avatar answered Oct 07 '22 02:10

WeiYuan