Add data in Sequelize migration script?

后端 未结 4 1935

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:35

    I figured it out. Sequelize is available from migration.migrator.sequelize. It is possible to do something like this:

    up: function (migration, DataTypes, done) {
        migration.createTable(
            'Person',
            {
                name: DataTypes.STRING,
                age: DataTypes.INTEGER,
            }
        ).success(function () {
            migration.migrator.sequelize.query("insert into person (name, age) values ('Donald Duck', 60)");
            done();
        });
    },
    down: function (migration, DataTypes, done) {
        migration.dropTable(
          'Person'
        ).then(function() {
          done();
        })
    }
    

提交回复
热议问题