Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add column in Sequelize existing model?

I have added a model and a migration file using this command

node_modules/.bin/sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string

Now I wanted to add few more fields like gender and age in to the existing table(model). I changed model manually and fire this command

node_modules/.bin/sequelize db:migrate

But it is responding that "No migrations were executed, database schema was already up to date. "

User.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  var User = sequelize.define('User', {
    firstName: DataTypes.STRING,
    lastName: DataTypes.STRING,
    email: DataTypes.STRING
  }, {});
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

Thank you in advance :)

like image 808
Ajay Poriya Avatar asked Apr 18 '18 04:04

Ajay Poriya


People also ask

How do I add columns in Sequelize?

To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file. Then we call addColumn to add a column and removeColumn to remove a column in the migration file. to create a migration file. in the migration file.

How do I change a column in Sequelize?

To Fix Sequelize js , how do we change column type in migration, you can use changeColumn instead of addColumn because addColumn will add new column in your table . you can define your migration like this : Migration File module. exports = { up: (queryInterface, Sequelize) => { return Promise. all([ queryInterface.

How do I update migration in Sequelize?

In your root folder execute the following command. npx sequelize-cli migration:create -- name create_schemas . This will tell sequelize to create a new migration. Executing this command will generate a file in our migrations-folder called something like this: '20210519183705-create_schemas.


2 Answers

In your sequelize initialization add alter property in true, and add column or association into your existing model file.

db.sequelize.sync({ force: false, alter: true })

From relevant documentation:

User.sync({ alter: true }) 

This checks what is the current state of the table in the database (which columns it has, what are their data types, etc), and then performs the necessary changes in the table to make it match the model.

like image 186
Cristian Gómez Avatar answered Sep 22 '22 13:09

Cristian Gómez


In order to add new fields to the table,we should use migration skeleton as shown below.

sequelize migration:create --name Users

Open the migration file and add the below codes

module.exports = {
  up: function (queryInterface, Sequelize) {
    return [ queryInterface.addColumn(
              'Users',
              'gender',
               Sequelize.STRING
             ),
            queryInterface.addColumn(
             'Users',
             'age',
             Sequelize.STRING
          )];
  },

  down: function (queryInterface, Sequelize) {
    // logic for reverting the changes
  }
};

Then just run the migration

node_modules/.bin/sequelize db:migrate

Note: The passed queryInterface object can be used to modify the database. The Sequelize object stores the available data types such as STRING or INTEGER.

Full list of methods in Query Interface

I hope this will help you. If you have any issues let me know.

like image 29
Suvethan Nantha Avatar answered Sep 18 '22 13:09

Suvethan Nantha