Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 668
Ulises Vargas De Sousa Avatar asked Sep 21 '16 16:09

Ulises Vargas De Sousa


People also ask

Why do we need migrations in Sequelize?

With migrations you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe how to get to the new state and how to revert the changes in order to get back to the old state. You will need the Sequelize Command-Line Interface (CLI).


2 Answers

-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
like image 50
Kusal Kithmal Avatar answered Oct 05 '22 11:10

Kusal Kithmal


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.

like image 29
tmquang6805 Avatar answered Oct 05 '22 11:10

tmquang6805