when i change my model and migration file and run sequelize db:migrate:undo and then sequelize db:migrate, changes wont be made on the database table.
Instead, the changes affect the last migration file / model file.
This question is similar to this one here : How to add column in sequelize existing model.
But anyways, a migration is simply a set of instructions using the queryInterface methods, so for example the default method that's being used when you generate the migration using the cli is the (createTable) method.
If you want to alter the table in any way you need to use another method for example (renameColumn: to rename a column, changeColumn: to change a column data type, ..etc)
For example if i want to rename a column called name
to firstName
on an already existing (migrated) Users
table i would create a new migration file to modify it and it would look like so :
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('Users', 'name', 'firstName', {
type: Sequelize.STRING,
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('Users', 'firstName', 'name', {
type: Sequelize.STRING,
})
}
};
And this is in an example of adding a new column (lastName) to the same table Users
:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('Users', 'lastName', {
type: Sequelize.STRING
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn("Users", "lastName")
}
};