modernator
modernator

Reputation: 4427

Sequelize running migration results Cannot read property 'key' of undefined

I wanted to update column to set not null to false, however running db:migration fails with this error message:

Cannot read property 'key' of undefined

Here's the code of migration:

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
   return queryInterface.changeColumn('Notes', 'title', {
     allowNull: false
   });
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.changeColumn('Notes', 'title', {
      allowNull: true
    });
  }
};

As followed document, it seems nothing wrong with my code.

Table and field are exists, what am I wrong?

Upvotes: 3

Views: 2809

Answers (3)

Jayesh Gatkal
Jayesh Gatkal

Reputation: 59

You must have to provide the column type either you want to change it or not.

'use strict';

module.exports = {
 up: (queryInterface, Sequelize) => {
  return queryInterface.changeColumn('Notes', 'title', {
    type: Sequelize.STRING,
    allowNull: false
  });
 },

 down: (queryInterface, Sequelize) => {
   return queryInterface.changeColumn('Notes', 'title', {
     type: Sequelize.STRING,
     allowNull: true
   });
 }
};

Upvotes: 0

AyanamiSan
AyanamiSan

Reputation: 133

Just had this problem myself. From what I see in the source code Sequelize assumes you always provide the type when changing the column. I see it's also in the documentation you linked: "Please make sure, that you are completely describing the new data type."

Upvotes: 2

kashish verma
kashish verma

Reputation: 61

Just a tip how you can fix it. I think query interface object is not able to find the name or column of the table which has to be migrated. Can you just print your Sequelize object and models inside it and put the same name which your Sequelize object holds. I have tried the same library and it works like a charm.

Upvotes: 1

Related Questions