I am trying to execute multiple migration statements in a single migration file in order to make changes to multiple columns of same table in one go.
I want to know that whether I am doing it in a write way or not or is there a better and more appropriate way to do it:
module.exports = {
up: function(queryInterface, Sequelize, done) {
queryInterface.changeColumn('users', 'name', {
type: Sequelize.STRING,
allowNull: false,
require: true,
unique: true
}).success(function() {
queryInterface.changeColumn('users', 'address', {
type: Sequelize.STRING,
allowNull: false,
require: true,
unique: true
}).success(function() {
queryInterface.changeColumn('users', 'city', {
type: Sequelize.STRING,
allowNull: false,
require: true,
unique: true
}).success(function() {
queryInterface.changeColumn('users', 'state', {
type: Sequelize.STRING,
allowNull: false,
require: true,
defaultValue: "ncjnbcb"
});
done();
});
});
});
}
};
But I face an error which says:
TypeError: undefined is not a function
Since i couldn't find any way of debugging error in migrations, it will be great if someone helps me out in resolving it or if possible, tell about the way as of how can we figure out the errors in a migration.
Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.
Node migrations are much like term migrations, in terms of mapping a source type to a destination type. source_type: The unique machine name of the legacy node type. destination_type: The unique machine name of the destination node type.
Your TypeError is probably because you're not returning anything. The docs say that each migration function should return a Promise. No mention of a done
callback.
To that end, try the following:
return Promise.all([
queryInterface.changeColumn...,
queryInterface.changeColumn...
]);
module.exports = {
up: async (queryInterface, Sequelize) => {
try {
await queryInterface.addColumn('User', 'name', {
type: Sequelize.STRING
});
await queryInterface.addColumn('User', 'nickname', {
type: Sequelize.STRING
});
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
},
down: async (queryInterface, Sequelize) => {
try {
await queryInterface.removeColumn('Challenges', 'name');
await queryInterface.removeColumn('Challenges', 'nickname');
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
}
};
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