Sean D
Sean D

Reputation: 4292

Sequelize migration: ERROR: Cannot read property 'toString' of undefined

My sequelize migration file being referenced in terminal:

module.exports = {
  up: (queryInterface, DataTypes) => Promise.all([
    queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false }),
  ]),
  down: (queryInterface, Sequelize) => {
    queryInterface.dropTable('Documents');
  },
};

/*
$ npx sequelize db:migrate

Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]

Loaded configuration file "config/config.js".
Using environment "development".
== 20210306192040-alter-documents2: migrating =======

ERROR: Cannot read property 'toString' of undefined
*/

Other SO posts have mentioned this may be caused by access to an invalid property on DataTYpes but I believe String is a valid property.

Are there any other mistakes above?

$ psql --version
psql (PostgreSQL) 12.6 (Ubuntu 12.6-0ubuntu0.20.04.1)

$ node --version
v14.15.4


// package.json
    "pg": "^8.5.1",
    "sequelize": "^6.5.0",
    "sequelize-cli": "^6.2.0"

I am copying my model file below in case that helps:

'use strict';
const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class Document extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  Document.init({
    id: {
      type: DataTypes.number,
      primaryKey: true,
      allowNull: false,
    },
    ownerDoc: {
      type: DataTypes.String,
      allowNull: false,
    },
    ownerName: {
      type: DataTypes.String,
      allowNull: false,
    },
    createdAt: DataTypes.Date,
    updatedAt: DataTypes.Date,
    deletdAt: {
      type: DataTypes.Date,
      allowNull: true,
    },
    uploadBy: {
      type: DataTypes.String, // not marked as optional in entity
      allowNull: true,
    },
    fileUrl: {
      type: DataTypes.String,
      allowNull: true, // not marked as optional in entity
    },
    category: {
      type: DataTypes.String,
      allowNull: false,
    },
    status: {
      type: DataTypes.String,
      allowNull: false,
    },
    fileType: {
      type: DataTypes.String,
      allowNull: true,
    }, // not marked as optional in entity
    version: {
      type: DataTypes.number,
      allowNull: false,
    },
    type: {
      type: DataTypes.String,
      allowNull: false,
    },
    source: {
      type: DataTypes.String,
      allowNull: false,
    },
    data: {
      type: DataTypes.JSON,
      allowNull: true,
    }, // any[],
    expiration: {
      type: DataTypes.Date,
      allowNull: true,
    },
    requestAgainExpiration: {
      type: DataTypes.number,
      allowNull: true,
    },
    titleSufix: {
      type: DataTypes.String,
      allowNull: true,
    }, // rename back if it causes trouble
    inputRequest: {
      type: DataTypes.JSON,
      allowNull: true,
    }, // any[],
    parentId: {
      type: DataTypes.String,
      allowNull: true,
    },
    borrowerId: {
      type: DataTypes.String,
      allowNull: true,
    },
    loanApplicationId: {
      type: DataTypes.String,
      allowNull: true,
    },
  }, {
    sequelize,
    modelName: 'Document',
  });
  return Document;
};

Upvotes: 0

Views: 2472

Answers (1)

Wang Liang
Wang Liang

Reputation: 4434

module.exports = {
  up: (queryInterface, DataTypes) => Promise.all([
    queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.STRING, allowNull: false }),
  ]),
  down: (queryInterface, Sequelize) => {
    queryInterface.dropTable('Documents');
  },
};
...
 id: {
      type: DataTypes.NUMBER, // Number instead of number
      primaryKey: true,
      allowNull: false,
    },
...

Upvotes: 2

Related Questions