Ulises Vargas De Sousa
Ulises Vargas De Sousa

Reputation: 847

My models for DB using Sequelize don't doing migration

I've two models:

user.js

'use strict'
module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define('User', {
    gid: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    },
    newsletters: {
      type: 'NUMERIC',
      allowNull: false,
      defaultValue: '1'
    },
    status: {
      type: 'NUMERIC',
      allowNull: false,
      defaultValue: '1'
    },
    date_verified: {
      type: DataTypes.TIME,
      allowNull: true
    },
    date_created: {
      type: DataTypes.TIME,
      allowNull: false,
      defaultValue: sequelize.fn('now')
    },
    date_updated: {
      type: DataTypes.TIME,
      allowNull: false,
      defaultValue: sequelize.fn('now')
    }
  },{
    tableName: 'user'
  },{
    classMethods:{
      associate: function(models){
        User.belongsTo(models.User);
      }
    }
  });

  User.schema("security");

  return User;
};

role.js

'use strict'
module.exports = function(sequelize, DataTypes) {
  var Role = sequelize.define('Role', {
    gid: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    status: {
      type: 'NUMERIC',
      allowNull: false,
      defaultValue: '1'
    },
    date_created: {
      type: DataTypes.TIME,
      allowNull: false,
      defaultValue: sequelize.fn('now')
    },
    date_updated: {
      type: DataTypes.TIME,
      allowNull: false,
      defaultValue: sequelize.fn('now')
    }
  },{
    tableName: 'role'
  },{
    classMethods:{
      associate: function(models){
        Role.hasMany(models.User);
      }
    }
  });

  Role.schema("security");

  return Role;
};

And index.js in the same "models" folder, that is generated automatically for Sequelize.

I only changed the config.json with my connection variables, and connects succefully.

But, when I put in console

node_modules/.bin/sequelize db:migrate

Shows me this:

Sequelize [Node: 4.4.4, CLI: 2.1.0, ORM: 3.12.2, pg: ^4.4.3]

Loaded configuration file "config\config.json".
Using environment "development".
Using gulpfile c:\Users\Ulises\MVO-app\server\node_modules\sequelize-cli\lib\gulpfile.js
Starting 'db:migrate'...
Finished 'db:migrate' after 180 ms
No migrations were executed, database schema was already up to date. 

And in my DB don't create the models

Upvotes: 13

Views: 13964

Answers (6)

Swornim Shah
Swornim Shah

Reputation: 69

Use command npx sequelize db:generate --name TABLE_NAME_OPERATION where TABLE_NAME_OPERATION may be userModel-add-email-column

Your database related changes should be like git commits where every changes should create new migration files rather than changing existing migration history.

For every change in database do the following steps:

  1. Create a new migration file: ``npx sequelize db:generate --name TABLE_NAME_OPERATION`
  2. In the newly generated file write the below db related migration code.
  3. Reflect changes in the database: npx seqeulize db:migrate

For example sequelize code (Javascript or Typescript)

    /** @type {import('sequelize-cli').Migration} */
    module.exports = {
      async up(queryInterface, Sequelize) {
        await queryInterface.addColumn('UserModels', 'status', {
          type: Sequelize.STRING,
          allowNull: true,
          unique: false
        })
      },

      async down(queryInterface, Sequelize) {
        await queryInterface.removeColumn('UserModels', 'status')
      }
    };

Upvotes: 0

user16733667
user16733667

Reputation:

this sequelize db:migrate:undo:all worked in my case

Upvotes: 0

Mehmet Erdem AKIN
Mehmet Erdem AKIN

Reputation: 1

I was coding the user registration application. but the columns were not coming into the database. I solved this problem using sequelize db:migrate:undo:all && sequelize db:migrate.

Upvotes: 0

Ulises Vargas De Sousa
Ulises Vargas De Sousa

Reputation: 847

This question was a long time ago, but in a fresh setup I'm configuring a script yarn migrate that stands for: sequelize db:migrate:undo:all && sequelize db:migrate && sequelize db:seed:all && node grantsSeeders.js, being grantsSeeders.js my grants for DB

Refresh all migrations successfully.

Upvotes: 2

Kusal Kithmal
Kusal Kithmal

Reputation: 1313

-This worked for me-

In your database, please find the table named 'SequelizeMeta' and remove the relevant record you want to migrate.

After that run this on your console.

$ npx sequelize-cli db:migrate

Upvotes: 23

tmquang6805
tmquang6805

Reputation: 266

Please check in your database, table SequelizeMeta, and remove record which corresponding name with file migration. Sequelize will log migration into this table, and when run migration again, it cannot re-run migration file.

Upvotes: 10

Related Questions