node.js - 在 Sequelize 迁移脚本中添加数据?

标签 node.js sequelize.js

如何在 Sequelize 迁移脚本中向表中添加数据? 这是我得到的:

module.exports = {
up: function(migration, DataTypes, done) {
    migration.createTable(
        'person',
        {
            name: DataTypes.STRING,
            age: DataTypes.INTEGER
        },
        {
            charset: 'latin1' // default: null
        }
    );
    // I want to insert person and age.
    migration.insert(???);
    done()
},
down: function(migration, DataTypes, done) {
    migration.dropTable('queue');
    done()
}

}

最佳答案

我想通了。 Sequelize 可从 migration.migrator.sequelize 获得。可以这样做:

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

关于node.js - 在 Sequelize 迁移脚本中添加数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18742962/

相关文章:

mysql - Sequelize - 表多元化错误,表中没有实例

postgresql - 如何使用 postgresql 删除所有带有 Sequelize.js 的表?

javascript - 密码 bcrypt 返回未定义

javascript - Sequelize limit 和 offset 查询中不正确的位置

node.js - React-typeahead - 不变违规 : addComponentAsRefTo(. ..) 问题

json - npm 安装后类型无法正常工作

javascript - 在 Node js中加载本地jquery.js文件

javascript - 如何从 JS 文件获取变量到 Node JS 文件

javascript - 尝试连续使用 async.forEach 但失败 - Node.js

sequelize.js - 如何使用存储在变量中的值进行 RAW 查询