在sequelize.js中尝试以下原始MySQL插入时:
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
});
);
生成的控制台日志如下所示:
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 } ]
我需要做什么才能获得以下数据的ID?甚至是最后插入的ID?
答案 0 :(得分:0)
您可以通过传递{ type: sequelize.QueryTypes.INSERT }
作为选项参数来实现此目的。这将使sequelize返回插入它。
简单查询示例:
sequelize.query(
"INSERT INTO clients (name) values ('myClient')",
{ type: sequelize.QueryTypes.INSERT }
).then(function (clientInsertId) {
console.log(clientInsertId);
});