Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return last inserted id sequelize

How could I send the last inserted id when creating a new entry in sequlize?

Currently this is the model way of creating a new value:

function post( request, response ) {

    models.xxx.create( {

            field1: request.body.field1,
            field2: request.body.field2

        } )
        .then( function ( x ) {

            //x = 0 || 1;
            response.json( x );
            //What I would like to send is the `id` of the new row

        } );

}

Is that possible with sequelize, or do I have to query the table?

like image 899
Jonathan Solorzano Avatar asked Oct 15 '25 16:10

Jonathan Solorzano


2 Answers

I am using MSSQL dialect and the return result is a row of new created data. Access object key "id" will show you the last increment id used.

.then( function ( x ) {

    //x = new created row
    response.json( x.id );

} );
like image 79
mike85 Avatar answered Oct 18 '25 05:10

mike85


Actually the promise function returns the newly created row, but for some reason I could only see [1] at my console, now I see the object.

.then( function ( x ) {

    //x = new created row
    response.json( x );

} );
like image 32
Jonathan Solorzano Avatar answered Oct 18 '25 05:10

Jonathan Solorzano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!