如何设置Sequelize以使用自动增量版本号?
根据文档,我只需要设置版本:true:http://docs.sequelizejs.com/manual/tutorial/models-definition.html#configuration
首次尝试:
import Sequelize from "sequelize";
export const sequelize = new Sequelize('my_connection', 'fake-user-name', 'fake-password', {
host: 'localhost',
dialect: 'postgres'
});
export const User = sequelize.define('user', {
id: {type: Sequelize.STRING, primaryKey: true},
name: {type: Sequelize.STRING, allowNull: false}
}, {
underscored: true,
tableName: 'r_users',
version: true // <- here
});
结果:没有创建版本列。
第二次尝试:
import Sequelize from "sequelize";
export const sequelize = new Sequelize('my_connection', 'fake-user-name', 'fake-password', {
host: 'localhost',
dialect: 'postgres'
});
export const User = sequelize.define('user', {
id: {type: Sequelize.STRING, primaryKey: true},
name: {type: Sequelize.STRING, allowNull: false},
version: { // <- here as well
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0
}
}, {
underscored: true,
tableName: 'r_users',
version: true
});
结果:创建了版本列,但在更新记录时它没有自动递增(我希望下面会增加版本):
User.find({where: {id: 1}}).then(item => {
item.name = "Bob";
item.save();
});
备注:
我正在使用postgres 9.6,我正在使用的库如下:
问题:
有谁可以帮我指出我做错了什么?
提前致谢。