Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update migration files when change column or add new column using Sequelize

I'm new in Sequelize, right now I'm creating a RESTful api with NodeJS and Sequelize. I'm trying to figure out how to change my Database Schema like change my Column name using Sequelize

I create a Model like this

sequelize model:create --name MyUser --attributes first_name:string,last_name:string,bio:text

It created a file in Models

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Page = sequelize.define('Page', {
    name: DataTypes.STRING,
    text: DataTypes.TEXT,
    url: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
      }
    }
  });
  return Page;
};

and one file in Migrations Folder

'use strict';
module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.createTable('Pages', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      name: {
        type: Sequelize.STRING
      },
      text: {
        type: Sequelize.TEXT
      },
      url: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable('Pages');
  }
};

The problem is how about if I want to add New column and change existing Column Name

Example I want to change to this

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Page = sequelize.define('Page', {
    fullname: DataTypes.STRING,
    text: DataTypes.TEXT,
    url: DataTypes.STRING,
    email: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
      }
    }
  });
  return Page;
};

I have read a few page in Stackoverflow about this, like in this page

How to auto generate migrations with Sequelize CLI from Sequelize models?

and

Sequelize.js: how to use migrations and sync

One of that page has a way to Alter Column automatic using Sequelize-cmd in this link https://www.youtube.com/watch?v=wHTBxtk8ezo but Sequelize-cmd is already deprecated and the other way and the only way I do now is create Migration File using sequelize migration:createand manually write a code to rename and add Column using addColumn and renameColumn

So, my question now is there a way to Creating Migration File with addColumn and renameColumn Automatic like what Sequelize-cmd do without have to write it manually ?

like image 272
Dirus Avatar asked Sep 21 '15 12:09

Dirus


1 Answers

First type sequelize migration:create --name changeColumn into your terminal after navigating to your project directory. This creates a new migration file in your migrations folder called changeColumn with the date of creation prepended. Then you use the renameColumn method which takes in the table name, original column name and the new column name. Once you have updated the file to the below code, go back to your terminal and type sequelize db:migrate to run the migration.

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.renameColumn('Users', 
  'beforeName', 'afterName');
 },

  down: (queryInterface, Sequelize) => {
    return queryInterface.renameColumn('Users', 'afterName', 'beforeName');
 }
};
like image 180
elarcoiris Avatar answered Sep 26 '22 14:09

elarcoiris