0
0

More than 5 years have passed since last update.

sequelizeでテーブルに変更を加える手順メモ

Last updated at Posted at 2019-09-04

version

Sequelize CLI [Node: 10.15.3, CLI: 5.4.0, ORM: 5.8.0]

テーブルに変更を加える

手順

  1. migrationファイルの作成
  2. 加えたい変更に合ったファイルの編集
  3. migrationの実行

1.migrationファイルの作成

以下のコマンドを入力すると、migrationファイルの雛形が生成される。

$ sequelize migration:create --name テーブル名

もしくは

$ sequelize migration:create

以下のファイルが生成されます。

'use strict'

module.exports = {
  up: (queryInterface, Sequelize) => {
    /*
      Add altering commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.createTable('users', { id: Sequelize.INTEGER });
    */
  },

  down: (queryInterface, Sequelize) => {
    /*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.dropTable('users');
    */
  }
}

upには、バージョンアップによる処理を記載する。
downには、ロールバックにより処理を記載する。

2. 加えたい変更に合ったファイルの編集

公式のマニュアルを参考に。
クエリ操作はここ
型についてはここ

複数の操作をする時は、transitionの記述も必要。

3. migrationの実行


$ sequelize db:migrate --env development

ロールバックはこれ

$ sequelize db:migrate:undo --env development
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0