Sequelize CLI : cannot read property 'replace' of undefined when migrating DB
Asked Answered
F

2

10

I am trying to migrate DB using db:migrate function, however I am getting the error printed:

ERROR: Cannot read property 'replace' of undefined

I have referred to other solutions for this error on GitHub but none resolve the issue. I am using sequelize-cli to perform migration.

here is my model:

    'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Containers', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      name: {
        type: Sequelize.STRING
      },
      userId: {
          type: Sequelize.INTEGER,
          references: "Users",
          refereceKey: "id",
          onUpdate: "cascade",
          onDelete: "cascade",
      },
      type: {
        type: Sequelize.STRING
      },
      detail: {
        type: Sequelize.INTEGER
      },
      checkin: {
        type: Sequelize.DATE
      },
      checkout: {
        type: Sequelize.DATE
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Containers');
  }
};

I have tried to execute other models by deleting this and nothing seems to work. Please help me here!

UPDATE

I have figured out that this is caused due to relations that I am trying to add, so I moved all the relations for all the posts in one separate migration:

    'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return [
        queryInterface.addColumn("Containers", "userId", {
            type: Sequelize.INTEGER,
            references: "Users",
            referenceKey: "id",
            onUpdate: "cascade",
            onDelete: "cascade"
        }),

        queryInterface.addColumn("Containers", "parentContainer", {
            type: Sequelize.INTEGER,
            references: "Containers",
            referenceKey: "id",
            onUpdate: "cascade",
            onDelete: "cascade"
        }),

        queryInterface.addColumn("Entities", "containerId", {
              type: Sequelize.INTEGER,
              references: "Containers",
              referenceKey: "id",
              onUpdate: "cascade",
              onDelete: "cascade",
        }),

        queryInterface.addColumn("Posts", "userId", {
                type: Sequelize.INTEGER,
                references: "Users",
                referenceKey: "id",
                onUpdate: "cascade",
                onDelete: "cascade"
        }),

        queryInterface.addColumn("Posts", "containerId", {
              type: Sequelize.INTEGER,
              references: "Containers",
              referenceKey: "id",
              onUpdate: "cascade",
              onDelete: "cascade",
              nullable: true
        }),

        queryInterface.addColumn("Votes", "userId", {
                type: Sequelize.INTEGER,
                references: "Users",
                referenceKey: "id",
                onUpdate: "cascade",
                onDelete: "cascade"
        }),

        queryInterface.addColumn("Votes", "postId", {
                type: Sequelize.INTEGER,
                references: "Posts",
                referenceKey: "id",
                onUpdate: "cascade",
                onDelete: "cascade"
        }),

        queryInterface.addColumn("Comments", "userId", {
                type: Sequelize.INTEGER,
                references: "Users",
                referenceKey: "id",
                onUpdate: "cascade",
                onDelete: "cascade"
        }),

        queryInterface.addColumn("Comments", "postId", {
                type: Sequelize.INTEGER,
                references: "Posts",
                referenceKey: "id",
                onUpdate: "cascade",
                onDelete: "cascade"
        }),

        queryInterface.addColumn("Comments", "commentId", {
                type: Sequelize.INTEGER,
                references: "Comments",
                referenceKey: "id",
                onUpdate: "cascade",
                onDelete: "cascade"
        }),
    ]
  },

  down: (queryInterface, Sequelize) => {

  }
};

Now, all the tables are created BUT, the error persists for this migration.

Fascism answered 18/2, 2019 at 22:38 Comment(0)
E
18

I had this same error.

Solution: I change

...

references: TABLE_NAME,
referenceKey: COLUMN_NAME

...

to

...

references: {
 model: TABLE_NAME,
 key: COLUMN_NAME

...

and it works

Electricity answered 12/4, 2019 at 20:28 Comment(1)
And do not confuse references.model with references.table from queryInterface.addConstraint. table option doesn't work with queryInterface.createTable and queryInterface.addColumn in Sequelize 6Aves
A
0

For me it was coming from my environment variables not being loaded while doing npx sequelize-cli db:create. When I managed to load it (with dot-env loading in my config.js file + dot-env cli to be sure) it worked fine.

If you use an IDE, you can climb to the definition that is being undefined. Or just check the slack trace.

To me, it was that line :

at getCreateDatabaseQuery (D:\Code\restaurant_project\website_engine\back_end\node_modules\sequelize-cli\lib\commands\database.js:82:64)

Leading to that line in the script (ctrl+click on the slack trace to go to the file)

return 'CREATE DATABASE IF NOT EXISTS ' + queryGenerator.quoteIdentifier(config.database)

config.database wasn't defineds, hence the error.

Apocarp answered 18/11, 2021 at 21:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.