更新表结构时需要用到 sqequlize 的migration(本文前提是已经创建好所有model)
参考:http://docs.sequelizejs.com/manual/migrations.html
准备工作:
- 安装 sequelize-cli
npm install --save sequelize-cli
- 在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')
}
- Use config/database.json file for config settings
- Use db/models as models folder
- Use db/seeders as seeders folder
- Use db/migrations as migrations folder
2. migrations文件
如果已经创建好model文件,则在migrations文件夹下创建.js
文件
3. 在服务端根目录下执行命令
// 执行修改
npx sequelize-cli db:migrate
// 撤销
npx sequelize-cli db:migrate:undo
网友评论