Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize migration fails by cannot read property 'toString' of undefined [closed]

When trying to run my migration on Sequelize, I am getting the following error;

== 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)

The migration file is as follows;

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

And my model file for this is;

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

I am at a loss as to what could be causing this error to occur as I have checked these files against other migrations that work successfully and can see no differences between them. I have gone as far as to clear out the entire database and remigrate, but this one is the only one that fails.

Thanks in advance.

like image 620
Harry Jarman Avatar asked Sep 04 '17 09:09

Harry Jarman


3 Answers

Figured out it was unrecognised data types.

like image 82
Harry Jarman Avatar answered Nov 16 '22 12:11

Harry Jarman


Make sure to use uppercase datatypes:

use DATE instead of Date or date

like image 41
AbdulRehman Avatar answered Nov 16 '22 14:11

AbdulRehman


Change DATETIME to DATE, there is no data type named DATETIME in sequelize. DATE works same as DATETIME, and DATEONLY works like DATE Details: http://docs.sequelizejs.com/variable/index.html

like image 14
Riajul Islam Avatar answered Nov 16 '22 13:11

Riajul Islam