My models for DB using Sequelize don't doing migration
Asked Answered
Z

6

13

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

Zinc answered 21/9, 2016 at 16:9 Comment(0)
D
23

-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
Disdainful answered 16/5, 2019 at 10:47 Comment(0)
S
10

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.

Societal answered 22/9, 2016 at 2:51 Comment(0)
Z
2

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.

Zinc answered 16/5, 2019 at 14:54 Comment(0)
B
0

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.

Bedside answered 11/4, 2021 at 22:12 Comment(0)
B
0

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

Bulgaria answered 4/10, 2021 at 23:30 Comment(0)
B
0

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')
      }
    };
Barbaresi answered 20/5, 2023 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.