Sequelize 迁移:错误:无法读取未定义的属性“toString”

时间:2021-03-06 21:48:28

标签: node.js postgresql sequelize.js

我的 sequelize 迁移文件在终端中被引用:

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
*/

其他 SO 帖子提到这可能是由于访问 DataTYpes 上的无效属性引起的,但我相信 String 是有效属性。

以上还有其他错误吗?

$ 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"

我正在下面复制我的模型文件,以防万一:

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

1 个答案:

答案 0 :(得分:1)

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,
    },
...