Sequelize 迁移失败,无法读取未定义的属性“toString”[关闭]

Posted

技术标签:

【中文标题】Sequelize 迁移失败,无法读取未定义的属性“toString”[关闭]【英文标题】:Sequelize migration fails by cannot read property 'toString' of undefined [closed] 【发布时间】:2018-02-12 13:08:13 【问题描述】:

尝试在 Sequelize 上运行迁移时,我收到以下错误;

== 20170904085107-kognitio-queue: migrating =======
TypeError: Cannot read property 'toString' of undefined
    at Object.attributeToSQL (/home/vagrant/insights-api/node_modules/sequelize/lib/dialects/mysql/query-generator.js:240:34)
    at Object.attributesToSQL (/home/vagrant/insights-api/node_modules/sequelize/lib/dialects/mysql/query-generator.js:306:45)
    at QueryInterface.createTable (/home/vagrant/insights-api/node_modules/sequelize/lib/query-interface.js:171:38)
    at Object.up (/home/vagrant/insights-api/lib/migrations/20170904085107-kognitio-queue.js:4:31)
    at constructor._exec (/home/vagrant/insights-api/node_modules/umzug/lib/migration.js:104:23)
    at constructor.up (/home/vagrant/insights-api/node_modules/umzug/lib/migration.js:69:17)
    at constructor.<anonymous> (/home/vagrant/insights-api/node_modules/umzug/index.js:124:28)
    at PassThroughHandlerContext.finallyHandler (/home/vagrant/insights-api/node_modules/bluebird/js/release/finally.js:57:23)
    at PassThroughHandlerContext.tryCatcher (/home/vagrant/insights-api/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/vagrant/insights-api/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/vagrant/insights-api/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/vagrant/insights-api/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/vagrant/insights-api/node_modules/bluebird/js/release/promise.js:693:18)
    at Promise._fulfill (/home/vagrant/insights-api/node_modules/bluebird/js/release/promise.js:638:18)
    at Promise._resolveCallback (/home/vagrant/insights-api/node_modules/bluebird/js/release/promise.js:432:57)
    at Promise._settlePromiseFromHandler (/home/vagrant/insights-api/node_modules/bluebird/js/release/promise.js:524:17)

迁移文件如下;

'use strict';
module.exports = 
    up: function(queryInterface, Sequelize) 
        return queryInterface.createTable('Kognitio_Queue', 
            queue_id: 
                allowNull: false,
                primaryKey: true,
                type: Sequelize.INTEGER
            ,
            queue_user: 
                allowNull: false,
                type: Sequelize.STRING(20)
            ,
            queue_query: 
                allowNull: false,
                type: Sequelize.LONG
            ,
            queue_added: 
                allowNull: false,
                type: Sequelize.DATETIME,
                defaultValue: Sequelize.NOW
            ,
            queue_executed: 
                allowNull: true,
                type: Sequelize.DATETIME
            ,
            queue_save_results: 
                allowNull: false,
                type: Sequelize.BOOLEAN
            ,
            queue_results_path: 
                allowNull: true,
                type: Sequelize.TEXT
            
        );
    ,
    down: function(queryInterface, Sequelize) 
        return queryInterface.dropTable('Kognitio_Queue');
    
;

我的模型文件是;

module.exports = (sequelize, DataTypes) => 
    const Kognitio_Queue = sequelize.define('Kognitio_Queue', 
        queue_id: 
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
        ,
        queue_user: 
            type: DataTypes.STRING(20),
            allowNull: false
        ,
        queue_query: 
            allowNull: false,
            type: DataTypes.LONG
        ,
        queue_added: 
            allowNull: false,
            type: DataTypes.DATETIME
        ,
        queue_executed: 
            allowNull: true,
            type: DataTypes.DATETIME
        ,
        queue_save_results: 
            allowNull: false,
            type: DataTypes.BOOLEAN
        ,
        queue_results_path: 
            allowNull: true,
            type: DataTypes.TEXT
        
    , 
        underscored: true,
        freezeTableName: true
    );
    return Kognitio_Queue;
;

我不知道什么可能导致此错误发生,因为我已将这些文件与其他成功运行的迁移进行了检查,并且看不到它们之间的差异。我已经清除了整个数据库并重新迁移,但这是唯一一个失败的。

提前致谢。

【问题讨论】:

【参考方案1】:

发现这是无法识别的数据类型。

【讨论】:

咳咳,续集里的错误捕捉太糟糕了 可以更详细地解释错误。 在某些情况下,错误类型被遗漏。就我而言:我想更新 allowNull 属性,但我错过了类型。希望能解决你的疑惑。 我的情况是,当我想更改列时我错过了类型,然后我通过添加类型来修复它,但是当我想撤消以前用于更改 gosh 的迁移时,我再次收到此错误 我的错误是我试图更改列以允许 null 但根本没有提供类型【参考方案2】:

确保使用大写数据类型:

使用 DATE 代替 Date 或日期

【讨论】:

谢谢!你救了我!【参考方案3】:

DATETIME改为DATE,sequelize中没有名为DATETIME的数据类型。 DATE 与 DATETIME 相同,DATEONLY 与 DATE 相同 详情:http://docs.sequelizejs.com/variable/index.html

【讨论】:

以上是关于Sequelize 迁移失败,无法读取未定义的属性“toString”[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

使用 Sequelize 获取“TypeError:无法读取未定义的属性‘findAll’”

为啥这些 Jest 测试失败了? TypeError:无法读取未定义的属性“应用”

JQMIGRATE:jQuery.browser 已弃用,无法读取未定义的属性“mozilla”

模块构建失败:TypeError:无法读取未定义的属性“sassLoader”

npm run build - 编译失败 - 无法读取未定义的属性“toLowerCase”

无法读取未定义的属性“代码”,解码 Firebase ID 令牌失败