id:在sequelize中创build新项目时为null

当我尝试创build一个新的对话项时,Sequelize会返回一个id: null的对象,即使在数据库中有一个有效的ID。 我怎样才能得到Sequelize返回最后插入的ID到新创build的项目?

 Conversation.create({ type: 'private', createdBy: 1, }).then(conversation => { reply(conversation); }); 

将返回

 { "type": "conversations", "id": null, "createdBy": 1, "created_at": "2016-03-18T01:47:48.000Z" } 

我的代码:

 const Conversation = model.define('Conversation', { id: { type: Sequelize.INTEGER, primaryKey: true, }, type: { type: Sequelize.ENUM, values: ['private', 'group'], validate: { isIn: ['private', 'group'], }, }, createdBy: { type: Sequelize.INTEGER, field: 'created_by', }, }, { tableName: 'conversations', timestamps: true, createdAt: 'created_at', updatedAt: false, getterMethods: { type: () => 'conversations', }, }); const User = model.define('User', { id: { type: Sequelize.INTEGER, primaryKey: true, }, firstName: { type: Sequelize.STRING, field: 'first_name', allowNull: false, }, lastName: { type: Sequelize.STRING, field: 'last_name', allowNull: true, }, email: { type: Sequelize.STRING, allowNull: false, }, profileImg: { type: Sequelize.STRING, field: 'profile_img', allowNull: false, }, password: Sequelize.STRING, }, { tableName: 'users', timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', getterMethods: { type: () => 'users', }, }); Conversation.belongsToMany(User, { foreignKey: 'conversation_id', otherKey: 'user_id', through: 'conversation_user', timestamps: false, }); User.belongsToMany(Conversation, { as: 'conversations', foreignKey: 'user_id', otherKey: 'conversation_id', through: 'conversation_user', timestamps: false, }); 

哟需要把id:字段中的autoIncrement: true

 id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, } 

就个人而言,我会build议跳过id列作为sequalize自动为您自动运行,并很好地工作。

希望能帮助到你 :)

问题是我的MySQL版本(5.6而不是5.7),更新它,现在我得到承诺中创build项目的ID。