4

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 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
in the link it says you can create migrations using this sequelize-cli model:generate --name
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.