Sequelize create方法返回生成的主键?

时间:2014-12-16 03:38:26

标签: node.js sequelize.js

我想使用create方法在列中添加数据。但无法找到生成的价值。 表

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'
});

数据库

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;

1 个答案:

答案 0 :(得分:1)

不要用下面的词语:

id:Sequelize.BIGINT

删除后,它可以正常工作。

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