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.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-cli db:migrate command looks on the SequelizeMeta table for any migration files which haven't run yet. Every migration lives on this table and inside migrations/ folder. This is the simplest migration you can create.
By default, sequelize stores the migration script name in the SequelizeMeta table so it can keep track of which migration has executed and which is not. However, we could change the default migration storage table name to something we prefer, in this example, we will change it to migrations.
sequelize-cli db:migrate command looks on the SequelizeMeta table for any migration files which haven't run yet. Every migration lives on this table and inside migrations/ folder.
To insert new rows into your SQL table, you need to first create a Sequelize Model object. In Sequelize, a Model is an object that represents your SQL table, giving information to Sequelize about the name, the columns, and their data types. From the Model, you can then call the create () method, which performs an INSERT SQL statement to your table.
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();
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With