Sequelize CLI:迁移数据库时无法读取未定义的属性“替换”

时间:2019-02-18 22:38:07

标签: node.js sequelize.js sequelize-cli

我正在尝试使用db:migrate函数迁移数据库,但是我得到了打印错误:

  

错误:无法读取未定义的属性“替换”

我已经在GitHub和SO上针对此错误引用了其他解决方案,但是似乎没有一个解决方案。我正在使用sequelize-cli执行迁移。

这是我的模特:

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

现在,我已尝试通过删除此代码来执行其他模型,但似乎没有任何效果。请在这里帮助我!

更新

我发现这是由于我要添加的关系引起的,所以我将所有帖子的所有关系移到了一个单独的迁移中:

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

  }
};

现在,所有表均已创建,但此迁移仍然存在错误。

1 个答案:

答案 0 :(得分:1)

我有同样的错误。

解决方案:我更改

...

references: TABLE_NAME,
referenceKey: COLUMN_NAME

...

...

references: {
 model: TABLE_NAME,
 key: COLUMN_NAME

...

它有效