DEV Community

Chinedu Orie
Chinedu Orie

Posted on

How to Add New Fields to Existing Sequelize Migration

Sometimes you realize that you need to add some new fields to an existing sequelize migrations. This is especially important for production apps where refreshing the migration is not an option. Below is a quick guide on how to implement it.

Let's assume you want to add some new fields to Users table; below are the steps.

Step 1 - Create a new migration

npx sequelize-cli migration:create --name modify_users_add_new_fields
Enter fullscreen mode Exit fullscreen mode

Step 2 - Edit the migrations to suit the need

module.exports = {
  up(queryInterface, Sequelize) {
    return Promise.all([
      queryInterface.addColumn(
        'Users', // table name
        'twitter', // new field name
        {
          type: Sequelize.STRING,
          allowNull: true,
        },
      ),
      queryInterface.addColumn(
        'Users',
        'linkedin',
        {
          type: Sequelize.STRING,
          allowNull: true,
        },
      ),
      queryInterface.addColumn(
        'Users',
        'bio',
        {
          type: Sequelize.TEXT,
          allowNull: true,
        },
      ),
    ]);
  },

  down(queryInterface, Sequelize) {
    // logic for reverting the changes
    return Promise.all([
      queryInterface.removeColumn('Users', 'linkedin'),
      queryInterface.removeColumn('Users', 'twitter'),
      queryInterface.removeColumn('Users', 'bio'),
    ]);
  },
};
Enter fullscreen mode Exit fullscreen mode

Step 3 - Update the model with the new fields

Step 4 - Run migration

npx sequelize-cli db:migrate

Enter fullscreen mode Exit fullscreen mode

That is it, you have successfully added new fields to an existing migration.

Oldest comments (16)

Collapse
 
germantellezv profile image
Germán Téllez Vanegas

Hi, great tutorial. Does this keep the data in the database? or does it remove all data?

Collapse
 
ebzeal profile image
Olusola Ajayi

"npx sequelize-cli db:migrate" keeps the data in the database. To wipe the database you'd need to do "npx sequelize-cli db:migrate:undo:all" before "npx sequelize-cli db:migrate"

Collapse
 
rdecker573 profile image
rdecker573

If a migration is rolled back, I think that will remove data.

Collapse
 
sammykapienga profile image
sammy kapienga

Keeps data

Collapse
 
msamgan profile image
Mohammed Samgan Khan

what fi i want to add the new field after a particular field.

Collapse
 
mohamedhamada profile image
Mohamed Hamada

Thanks very much !
there is no much tutorials for these things out there!

Collapse
 
mwibutsa profile image
Mwibutsa Floribert

Thanks, this was helpful

Collapse
 
saikat_banerjee_dd614d16b profile image
Saikat Banerjee

How to maintain sequence of the coloumn to be added while creating the coloumn. if I need to add the coloumn after a particular coloumn which already exists. By the above implementation the coloumn will always be added at the end of the list.

Collapse
 
guryashzone profile image
Guryash Singh

How can we add a column after specific column?

Collapse
 
dmikester1 profile image
Mike Dodge

Like this:
return Promise.all([
queryInterface.addColumn(
'users', // table name
'verifier', // new field name
{
type: Sequelize.STRING,
allowNull: true,
after: 'manager_bonus',
}
),
]);

Collapse
 
nicola_lauritano_93149f83 profile image
Nicola Lauritano

Hi all,

When I run npx sequelize-cli db:migrate I got this error:

Cannot find "{myPath}/config/config.json". Have you run "sequelize init"?

I don't have a config.json file, should I create it or maybe it should be created by npx sequelize-cli migration:create --name modify_users_add_new_fields ?

If I have to create, how it should be?

Thanks all guys

Collapse
 
vladorjiggy profile image
vladorjiggy

This tutorial describes how to extend a existing migration. If you haven't used the CLI for your project, you have to initialize it by npx sequelize init

Collapse
 
let_mikev profile image
Miguel V

Luv ya Chinedu!

Collapse
 
miracle_ profile image
Miracle Nwaubani

@nedsoft please I want to ask how can one generate migration from existing model, say you have model in user.js file and you already defined your user model

`/* eslint-disable no-unused-vars */
'use strict';
const {  Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class User extends Model {}

  User.init({
    first_name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    last_name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, 
  {
    sequelize,
    modelName: 'User',
    tableName: 'Users'
  });
  return User;
};`
Enter fullscreen mode Exit fullscreen mode

how do I now generate migration base on this existing model?

Collapse
 
ankurbansal18 profile image
Ankur Bansal

Sequelize supports a feature called "model migrations," where you can generate and apply migrations directly from your Sequelize model definitions. This approach simplifies the process of making schema changes. You can use the npx sequelize migration:generate command with the --name option to create a migration based on changes in your models. For example:

npx sequelize migration:generate --name update-models

This command will generate a migration file with changes based on the differences between your models and the current database schema. You can then run the migration as usual.

Note that this approach requires you to have well-defined Sequelize models that accurately represent your database schema.

Collapse
 
mananabdul1 profile image
Abdul Manan

So, if we have to edit then we have to change in file that we created, or just use cli for edit in any file?