2

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;
};

1 Answers1

0

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

billobeng
  • 156
  • 6
  • 2
    I have seen that URL but that is for running migrations not creating. I need to create them automatically. Now I have to create them manually every time I make changes to my models – mohammad hosein moti Dec 06 '20 at 04:52
  • in the link it says you can create migrations using this sequelize-cli model:generate --name – billobeng Dec 06 '20 at 07:22
  • https://stackoverflow.com/questions/27835801/how-to-auto-generate-migrations-with-sequelize-cli-from-sequelize-models this link will help you. – aum trivedi Jan 19 '21 at 17:25
  • 1
    there is no way to automatically generate initial migrations from existing models in sequelize-cli. Looks like author of the answer not even checked info he suggests to use – MadaShindeInai Feb 06 '22 at 15:45