【问题标题】:How to bulk insert using sequelize and postgres如何使用 sequelize 和 postgres 批量插入
【发布时间】:2016-03-31 18:59:54
【问题描述】:

在这里,我尝试在我的 sequelize db 中插入批量数据,但它会产生问题。 如何在angularjs中使用sequelize和postgres批量插入?

【问题讨论】:

  • 我尝试了 sequelize cli,但缺少列出我的种子标志

标签: sequelize.js


【解决方案1】:

首先使用sequelize seed:create --name nameofseed创建种子文件,它将在你的根目录中创建种子文件夹,在config.jsdevelopment:{seederStorage:'sequelize'}中添加seederStorage:'sequelize' 在这里给表名是offer,但在db中它是offers和json对象中的表字段。 创建播种机后,它需要分别migraterun。 这是运行种子的命令:sequelize db:seed:all,然后运行迁移命令:sequelize db:migrate 就是这样。

 module.exports = {
      up: function(queryInterface, Sequelize) {
        queryInterface.bulkInsert('offers', [{
          ban: [1],
          sDate: '2016-03-31T08:00:10.354Z',
          eDate: '2016-03-31T08:00:10.354Z',
          isActive: true,
          reminder: false,
          sold: false,
          couponFor: 'Coupon Code',
          couponVal: 'Flat20%',
          createdAt: '2016-03-31T08:00:10.354Z',
          updatedAt: '2016-03-31T08:00:10.354Z'
        },
        {
          ban: [1, 2],
          sDate: '2016-03-31T08:00:10.354Z',
          eDate: '2016-03-31T08:00:10.354Z',
          isActive: true,
          reminder: false,
          sold: false,
          couponFor: 'Coupon Code',
          couponVal: 'Flat40%',
          createdAt: '2016-03-31T08:00:10.354Z',
          updatedAt: '2016-03-31T08:00:10.354Z'
        },
        {
          ban: [1, 2],
          sDate: '2016-03-31T08:00:10.354Z',
          eDate: '2016-03-31T08:00:10.354Z',
          isActive: true,
          reminder: false,
          sold: false,
          couponFor: 'Coupon Code',
          couponVal: 'Flat60%',
          createdAt: '2016-03-31T08:00:10.354Z',
          updatedAt: '2016-03-31T08:00:10.354Z'
        },
        {
          ban: [1],
          sDate: '2016-03-31T08:00:10.354Z',
          eDate: '2016-03-31T08:00:10.354Z',
          isActive: true,
          reminder: false,
          sold: false,
          couponFor: 'Coupon Code',
          couponVal: 'Flat100%',
          createdAt: '2016-03-31T08:00:10.354Z',
          updatedAt: '2016-03-31T08:00:10.354Z'
        }], {});
        /*
          Add altering commands here.
          Return a promise to correctly handle asynchronicity.

          Example:
          return queryInterface.bulkInsert('Person', [{
            name: 'John Doe',
            isBetaMember: false
          }], {});
        */
      },

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

          Example:
          return queryInterface.bulkDelete('Person', null, {});
        */
      }
    };

【讨论】:

  • 如果 development:{seederStorage:'sequelize'} 没有在 config.js 中设置也可以
  • 我可以在迁移文件中使用 bulkInsert() 吗?
  • 您能否添加任何续集的示例或文档链接?提前致谢。
  • @maxwell 是的,你可以
猜你喜欢
  • 2019-07-15
  • 2015-06-10
  • 2015-03-31
  • 1970-01-01
  • 2014-07-23
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
相关资源
最近更新 更多