Sequelize db:seed在mysql方言中失败

时间:2018-10-15 16:23:01

标签: mysql migration associations sequelize.js sequelize-cli

我正在尝试使用**sequelize db:seed:all**将数据播种到现有的mysql表中。我在终端中收到以下错误,并且表中未插入任何数据:

Sequelize CLI [Node: 8.9.4, CLI: 4.1.1, ORM: 4.38.1]

Loaded configuration file "config\config.json".
Using environment "development".

ERROR: connect ETIMEDOUT

这是我的播种者

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {

      return queryInterface.bulkCreate('permissions', [


        {
          Name:'_create_user',Description:'User can create new user account',createdAt:new Date(),updatedAt:new Date()
        },
         {
          Name:'_create_category',Description:'User can create new  category',createdAt:new Date(),updatedAt:new Date()
        },

         {
          Name:'_edit_comment',Description:'User can edit own comments',createdAt:new Date(),updatedAt:new Date()
        },
        {
          Name:'_edit_category',Description:'User can edit course category',createdAt:new Date(),updatedAt:new Date()
        },

         {
          Name:'_create_permission',Description:'User can create new user permission',createdAt:new Date(),updatedAt:new Date()
        },
         {
          Name:'_create_role',Description:'User can create new user role',createdAt:new Date(),updatedAt:new Date()
        },
        {
          Name:'_assign_role',Description:'User can assign role to other users',createdAt:new Date(),updatedAt:new Date()
        },
         {
          Name:'_assign_permission',Description:'User can assign user permission to other users',createdAt:new Date(),updatedAt:new Date()
        },
         {
          Name:'_revoke_role',Description:'User can remove role assigned to a user',createdAt:new Date(),updatedAt:new Date()
        },
         {
          Name:'_create_permission',Description:'User can remove permission assigned to a user',createdAt:new Date(),updatedAt:new Date()
        }




        ], {});

  },

  down: (queryInterface, Sequelize) => {

      return queryInterface.bulkDelete('permissions', null, {});

  }
};

我的服务器运行正常。除sequelize db:seeddb:migrate

之外,其他对数据库的请求也可以正常工作

迁移仅适用于新表,但是更新现有表的迁移因相同错误而失败。

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {


     return queryInterface.addColumn(
      'Category', // name of Source model
      'ParentId', // name of the key we're adding 
      {
        type: Sequelize.INTEGER,
        references: {
          model: 'Category', // name of Target model
          key: 'id', // key in Target model that we're referencing
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
      }
    );
  },

  down: (queryInterface, Sequelize) => {
    /*
      Add reverting commands here.
      Return a promise to correctly handle asynchronicity.

      Example:
      return queryInterface.dropTable('users');
    */

    return queryInterface.removeColumn(
      'Category', // name of Source model
      'ParentId' // key we want to remove
    );
  }
};

运行上述迁移失败,并出现相同的错误,并且没有将新列添加到类别表。

此外,模型中定义的关系不起作用。查询包含关联的模型将再次失败,

{
    "error": {
        "name": "SequelizeEagerLoadingError"
    }
}

这是类别和产品两个模型

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Category = sequelize.define('Category', {
    CategoryName: DataTypes.STRING
  }, {});
  Category.associate = function(models) {
    // associations can be defined here
   Category.hasMany(models.Product,{foreignKey:'CategoryId'})

//也尝试过以下方法:Category.hasMany(models.Product); //在产品迁移中添加外键引用,而无需使其工作

  };
  return Category;
};

我已详细介绍了这些详细信息,以便有人可以帮助我找到这三个序列化问题的解决方案: 1)db:在现有表上迁移不起作用; 2)db:seed:all都无法正常工作; 3)协会不成立。

0 个答案:

没有答案