node+mysql+sequelize数据库表更新

更新表结构时需要用到 sqequlize 的migration(本文前提是已经创建好所有model)
参考:http://docs.sequelizejs.com/manual/migrations.html
准备工作:

  1. 安装 sequelize-cli
npm install --save sequelize-cli
  1. 在models同级文件下新建migrations

1. touch .sequelizerc

创建 .sequelizerc 文件,配置相关数据

const path = require('path');

module.exports = {
  'config': path.resolve('config', 'database.json'),
  'models-path': path.resolve('db', 'models'),
  'seeders-path': path.resolve('db', 'seeders'),
  'migrations-path': path.resolve('db', 'migrations')
}
  1. Use config/database.json file for config settings
  2. Use db/models as models folder
  3. Use db/seeders as seeders folder
  4. Use db/migrations as migrations folder

2. migrations文件

如果已经创建好model文件,则在migrations文件夹下创建.js文件

3. 在服务端根目录下执行命令

 // 执行修改
 npx sequelize-cli db:migrate

 // 撤销
 npx sequelize-cli db:migrate:undo

你可能感兴趣的:(node+mysql+sequelize数据库表更新)