Arthur Zhang
Arthur Zhang

Reputation: 1537

Sequelize create method return generated primary key?

I want to use create method to add data in the column. But could not found the generated value. table

var User = sequelize.define('User', {
    id:Sequelize.BIGINT,
    user_name: Sequelize.STRING,
    user_pass: Sequelize.STRING,
    is_vertify:Sequelize.BOOLEAN
}, {
    tableName:'weshop_admin_user',
    createdAt:'create_time',
    updatedAt:'update_time'
});

database

CREATE TABLE `weshop_admin_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(20) NOT NULL,
  `user_pass` varchar(20) NOT NULL,
  `create_time` timestamp NOT NULL DEFAULT '1990-01-01 00:00:00',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_vertify` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

Upvotes: 1

Views: 16189

Answers (1)

Arthur Zhang
Arthur Zhang

Reputation: 1537

Don't need the words below:

id:Sequelize.BIGINT

After removing, it works.

User.create(user, {isNewRecord:true}).complete(function(err, result) {
    if(err) {
        callback(0);
    } else {
        callback(result.id);    // This is generate primary key.
    }
})

Upvotes: 2

Related Questions