Add data in Sequelize migration script?

后端 未结 4 1943

How can I add data to a table in a Sequelize migration script? This is what I got:

module.exports = {
up: function(migration, DataTypes, done) {
    migration.cr         


        
4条回答
  •  别那么骄傲
    2021-02-07 07:31

    Actually it is not a good idea to just run a query. queryInterface has create() and bulkCreate() now.

    'use strict';
    module.exports = {
      up: function(queryInterface, Sequelize) {
        return queryInterface.bulkInsert('roles', [{
          label: 'user',
          createdAt: new Date(),
          updatedAt: new Date()
        }, {
          label: 'admin',
          createdAt: new Date(),
          updatedAt: new Date()
        }]);
      },
      down: function(queryInterface, Sequelize) {
        return queryInterface.bulkDelete('roles', null, {});
      }
    };
    

    This returns a promise as expected by sequelize-cli.

    Source (adapted): https://github.com/sequelize/sequelize/issues/3210

提交回复
热议问题