mohammad hosein moti
mohammad hosein moti

Reputation: 41

How to generate migrations automatically from Sequelize models

I just started to program with node.js. I needed an ORM so I used Sequelize library. I wrote my User model as follows and I need to create a migration for it. Is there any way to create migrations automatically from the Sequelize models instead of writing them on my own? I tried package sequelize-auto-migrations and run npx makemigration --name create_users but it returns error:

Unexpected token {

I'm using node v10.19.3

This is my Sequelize model:

const Model = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    getFullname() {
      return [ this.firstname, this.lastname ].join(' ');
    }
  }
  User.init({
    firstName: { type: DataTypes.STRING, allowNull: false },
    lastName: { type: DataTypes.STRING, allowNull: false },
    email: { type: DataTypes.STRING, allowNull: false, unique: true },
    national_number: { type: DataTypes.STRING, unique: true },
    phone_number: { type: DataTypes.STRING, allowNull: false, unique: true },
    username: { type: DataTypes.STRING, allowNull: false, unique: true },
    join_date: { type: DataTypes.DATEONLY, defaultValue: sequelize.NOW },
    last_login: { type: DataTypes.DATE },
    password: { type: DataTypes.STRING, allowNull: false },
    active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true }
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};

Upvotes: 4

Views: 5279

Answers (1)

billobeng
billobeng

Reputation: 166

You might want to try sequelize-cli instead. To run migrations you have to run this command npx sequelize-cli model:generate This link on sequelize.com should help https://sequelize.org/master/manual/migrations.html

Upvotes: 0

Related Questions