如何设置sequelizejs以使用版本列?

时间:2017-06-13 13:08:17

标签: postgresql sequelize.js

如何设置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,我正在使用的库如下:

  • “sequelize”:“^ 3.30.4”
  • “pg”:“^ 6.2.3”,
  • “pg-hstore”:“^ 2.3.2”,

问题:

有谁可以帮我指出我做错了什么?

提前致谢。

0 个答案:

没有答案