Get last inserted id Sequelize
Yes your answer is working. Another way would be
var Sequelize = require("sequelize")
var sequelize = new Sequelize('test', 'root', 'root', {dialect: 'mysql'})
var Page = sequelize.define( 'page', {
type : {type: Sequelize.STRING(20)},
order : {type: Sequelize.INTEGER, defaultValue: 1}
},{
timestamps: false,
underscored: true
})
Page.__factory = {autoIncrementField: 'id'}
Page.id = ''
sequelize.query('INSERT INTO pages SET `type` = ?, `order` = ?, `topic_version_id` = ?', Page, {raw: false}, ['TEXT', 1, 1] ).success(function(page) {
console.log(page)
Page.find(page.id)
.success(function(result){
console.log(result)
})
})
You have to add autoIncrement property in model definition.
const Article = sequelize.define('articles', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
}, {},
{
createdAt: false,
updatedAt: false
});
Then, you can access last inserted id with property in model definition.
Article.create(article)
.then(result => console.log(result.id));