DEV Community

Cover image for Modifying an existing sequelize migration

Modifying an existing sequelize migration

Anayo Samson Oleru on May 12, 2020

If you've just started using sequelize and sequelize CLI in development, you definitely had frequent addition and deletions of columns. And yeah, a...
Collapse
 
amitkum66494760 profile image
Amit Yadav •

for sequelize 6.3.0 version -
$ sequelize migration:create --name name_of_your_migration
this command is not working for create a migration file, then you can use-
$ sequelize migration:generate --name name_of_your_migration

Collapse
 
phillipiscoding profile image
Phillip Harden •

What if all I need to do is change the attribute name, like april, to something different?

Collapse
 
anayooleru profile image
Anayo Samson Oleru • • Edited

Hi @phillipiscoding my bad, I didn't get notified about this comment.

To change the attribute name, you use the renameColumn method, this is an example below:

module.exports = {
up: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.renameColumn('users', 'april', 'new_column_name'),
]);
}

down: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.renameColumn('users', 'new_column_name', 'april'),
]);
},
}

Collapse
 
hitarth1 profile image
Hitarth1 •

How to perform soft delete in sequelize?

Collapse
 
anayooleru profile image
Anayo Samson Oleru •

Hi Hitarth1, you need to define a model as a paranoid. You can do that by passing theparanoid: true option when you're defining your model. And make sure timestamps is not false. Paranoid needs timestamp to work.

Collapse
 
rogereiddir profile image
rogeraidr •

don't forget to add deletedAt column

Collapse
 
johnnychang609 profile image
johnny chang •

Thanks, your information. this help me a lot

Collapse
 
vamshinandan profile image
vamshinandan •

Hi i am having trouble adding boolean column, i am able to add the column but unable to insert the value into the column
Can you provide an example for it please

Collapse
 
anayooleru profile image
Anayo Samson Oleru •

Hi @vam, here is an example.

To add a Boolean column(Model):

is_activated: {
  type: DataTypes.BOOLEAN,
  defaultValue: false,
},

To insert a value into the column:

By default, it will always be False, but if you want to change it to true, pass true as the value. I mean a boolean true

example:

User.create({
is_activated: true
});

Collapse
 
anayooleru profile image
Anayo Samson Oleru •

Hi @Vam, here is an example.

To add a Boolean column(Model):

is_activated: {
  type: DataTypes.BOOLEAN,
  defaultValue: false,
},

To insert a value into the column:

By default, it will always be False, but if you want to change it to true, pass true as the value. I mean a boolean true

example:

User.create({
is_activated: true
});

Collapse
 
jeannitonmnr profile image
Monero Jeanniton •

Thank you

Collapse
 
sahla profile image
Sahl Khalifa •

Thank you. This was helpful. Keep it up