javascript - Sequelize迁移错误: Cannot read property 'length' of undefined

标签 javascript mysql node.js express sequelize.js

这是我遵循的教程:https://medium.com/@prajramesh93/getting-started-with-node-express-and-mysql-using-sequelize-ed1225afc3e0

这是使用 express + mysql 的 node js 项目,我在其中使用 ORM Sequelize。

尝试运行 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');
  }
};

最佳答案

问题出在 NUMBER 数据类型上。在 Sequelize 的 DataTypes 列表中找不到哪个 ( https://sequelize.org/master/manual/model-basics.html#data-types )

更改以下内容:

salary: {
            type: Sequelize.NUMBER
        }

到:

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

还要记得更新模型相关的数据类型。

关于javascript - Sequelize迁移错误: Cannot read property 'length' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53291677/

相关文章:

javascript - ajax 速度 - 简化多个 ajax 调用

java - 如何使用java运行mysql存储过程

mysql - 如何在一个查询中使用带有两个条件的mysql函数 "min()"

node.js - 如何从nodejs模块导出函数获取回调数据

node.js - 使用 Jade 模板局部变量设置输入值

javascript - 有条件地遍历数组

javascript - 在函数事件(onChange)中调用函数,来自在Leaflet和Vue.js中创建的Select

javascript - NodeJS - 发送到客户端后无法设置 header

php - 复选框 "checked"值与 session 和数据库值冲突 PHP

javascript - 通过额外数据扩展 promise