How to get last inserted ID of a raw query?

See original GitHub issue

I’m trying to get the last inserted ID of this query:


mysql_sequelize.query(
    'INSERT IGNORE INTO steamer(steamer_id, prov_id, name, avatar) VALUES(UNHEX(REPLACE(UUID(),\'-\',\'\')), \'' + prov_id + '\', \'' + steamer_name + '\', \'' + steamer_avatar + '\')',
).then(function (data) {
    console.log('inserted STEAMER data---> ',data)  // not very helpful info below
});

But I get a pretty useless response back:

inserted STEAMER data--->  [ OkPacket {
    fieldCount: 0,
    affectedRows: 1,
    insertId: 0,
    serverStatus: 2,
    warningCount: 1,
    message: '',
    protocol41: true,
    changedRows: 0 },
  OkPacket {
    fieldCount: 0,
    affectedRows: 1,
    insertId: 0,
    serverStatus: 2,
    warningCount: 1,
    message: '',
    protocol41: true,
    changedRows: 0 } ]

I just want the affected row’s ID (it’s a binary) so I can throw it into a variable and use it for a subsequent query.

At this point, you may be saying “well give it a model” that looks like this maybe:

mysqlSteamer = mysql_sequelize.define('steamer', {
                    'steamer_id': {
                        primaryKey: true,
                        type: Sequelize.BLOB(16)
                    },
                    'prov_id': Sequelize.INTEGER(6),
                    'name': Sequelize.STRING(250),
                    'avatar': Sequelize.STRING
                }, {
                    tableName: 'steamer'
                }),

but I don’t think that’s right because I get this error in response:

TypeError: valueSets.map is not a function
    at Model.bulkBuild (/Users/sadpanda/git/my_repo/node_modules/sequelize/lib/model.js:1792:20)

Ideas?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
janmeiercommented, Feb 29, 2016

You can pass { type: sequelize.QueryTypes.INSERT } as the options argument - this will tell sequelize to return the insert it. Or you could use the insertId or the result you get back (in the case of your example it’s probably 0 because the insert is ignored

0reactions
amdpcommented, Apr 6, 2019

router.post(“/link”, (req, res) => { const rowData = { autoincrementId: req.body.id, name: req.body.name } yourModel.create(rowData) .then(row => { sequelize.query(‘SELECT @@IDENTITY’, {type: Sequelize.QueryTypes.SELECT}) .then(id => console.log(id); res.json({id: id[0][‘@@IDENTITY’] }) })); …

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to you get the last inserted ID with a raw query using ...
You can achieve this by passsing { type: sequelize.QueryTypes.INSERT } as a option argument. This will make sequelize return the insert it.
Read more >
PHP MySQL Get Last Inserted ID - W3Schools
If we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated...
Read more >
How To Get Last Inserted ID On SQL Server - BeanSoftware
To get an ID of last inserted record, you can use this T-SQL: INSERT INTO Persons (FirstName) VALUES ('Joe');
Read more >
4 ways to get last inserted id of a MySQL table - thisPointer
4 ways to get last inserted id of a MySQL table ; * FROM TABLE employee_designation;. SELECT * FROM TABLE employee_designation; ; SELECT...
Read more >
How to Retrieve MySQL Last Insert ID in CodeIgniter 4
There are times as a Developer you need to capture the Last Insert ID value from a MySQL table column defined with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found