admin管理员组文章数量:1355555
When trying the following raw MySQL insert in sequelize.js:
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); // no useful info
});
);
The resulting console log looks like the following:
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 } ]
What do I have to do to get following data's ID? or even the last inserted ID?
When trying the following raw MySQL insert in sequelize.js:
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); // no useful info
});
);
The resulting console log looks like the following:
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 } ]
What do I have to do to get following data's ID? or even the last inserted ID?
Share Improve this question edited Feb 28, 2016 at 23:13 Brandon Minton asked Feb 28, 2016 at 23:04 Brandon MintonBrandon Minton 1,0144 gold badges15 silver badges26 bronze badges 1- 1 year later, sorry Iam late :D – lin Commented Apr 25, 2017 at 16:23
1 Answer
Reset to default 6You can achieve this by passsing { type: sequelize.QueryTypes.INSERT }
as a option argument. This will make sequelize return the insert it.
Simple query example:
sequelize.query(
"INSERT INTO clients (name) values ('myClient')",
{ type: sequelize.QueryTypes.INSERT }
).then(function (clientInsertId) {
console.log(clientInsertId);
});
本文标签: javascriptHow to you get the last inserted ID with a raw query using SequelizeStack Overflow
版权声明:本文标题:javascript - How to you get the last inserted ID with a raw query using Sequelize? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743982266a2571132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论