Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize migration error: Cannot read property 'length' of undefined

This is the tutorial I followed: https://medium.com/@prajramesh93/getting-started-with-node-express-and-mysql-using-sequelize-ed1225afc3e0

This is node js project using express + mysql where I use and ORM Sequelize.

I get this error when trying to run sequelize db:migrate

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Employees', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      name: {
        type: Sequelize.STRING
      },
      designation: {
        type: Sequelize.STRING
      },
      salary: {
        type: Sequelize.NUMBER
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      companyId: {
        type: Sequelize.NUMBER,
        onDelete: 'CASCADE',
        references: {
          model: 'Companies',
          key: 'id',
          as: 'companyId',
        }
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Employees');
  }
};
like image 352
MrClemRkz Avatar asked Nov 14 '18 00:11

MrClemRkz


1 Answers

Problem was relying on NUMBER DataType. Which was not found in the list of DataTypes of Sequelize ( https://sequelize.org/master/manual/model-basics.html#data-types )

Change the following:

salary: {
            type: Sequelize.NUMBER
        }

to:

salary: {
            type: Sequelize.DECIMAL(10, 2)
        }

Also remember to update DataType the model related.

like image 133
MrClemRkz Avatar answered Sep 16 '22 13:09

MrClemRkz