Reputation: 1043
I'm new in Sequelize, right now I'm creating a RESTful api with NodeJS and Sequelize. I'm trying to figure out how to change my Database Schema like change my Column name using Sequelize
I create a Model like this
sequelize model:create --name MyUser --attributes first_name:string,last_name:string,bio:text
It created a file in Models
'use strict';
module.exports = function(sequelize, DataTypes) {
var Page = sequelize.define('Page', {
name: DataTypes.STRING,
text: DataTypes.TEXT,
url: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Page;
};
and one file in Migrations Folder
'use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('Pages', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
text: {
type: Sequelize.TEXT
},
url: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable('Pages');
}
};
The problem is how about if I want to add New column and change existing Column Name
Example I want to change to this
'use strict';
module.exports = function(sequelize, DataTypes) {
var Page = sequelize.define('Page', {
fullname: DataTypes.STRING,
text: DataTypes.TEXT,
url: DataTypes.STRING,
email: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Page;
};
I have read a few page in Stackoverflow about this, like in this page
How to auto generate migrations with Sequelize CLI from Sequelize models?
and
Sequelize.js: how to use migrations and sync
One of that page has a way to Alter Column automatic using Sequelize-cmd in this link https://www.youtube.com/watch?v=wHTBxtk8ezo but Sequelize-cmd is already deprecated and the other way and the only way I do now is create Migration File using sequelize migration:create
and manually write a code to rename and add Column using addColumn
and renameColumn
So, my question now is there a way to Creating Migration File with addColumn
and renameColumn
Automatic like what Sequelize-cmd
do without have to write it manually ?
Upvotes: 10
Views: 10936
Reputation: 978
Simplest way to do this is
queryInterface.renameColumn('tableName', 'oldName', 'newName');
NOTE: I tries this in down function but not work it's working once I write this code in up function
Upvotes: 1
Reputation: 192
There is a new npm package for solving this problem by comparing your models files and writing migration files for you
Sequelize-mig
Install it with:
npm install sequelize-mig -g / yarn global add sequelize-mig
then use it like this
sequelize-mig migration:make -n <migration name>
and it will auto generate the migration file for you with all updates read from your models files
Upvotes: 0
Reputation: 1976
First type sequelize migration:create --name changeColumn
into your terminal after navigating to your project directory. This creates a new migration file in your migrations folder called changeColumn with the date of creation prepended.
Then you use the renameColumn method which takes in the table name, original column name and the new column name. Once you have updated the file to the below code, go back to your terminal and type sequelize db:migrate
to run the migration.
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('Users',
'beforeName', 'afterName');
},
down: (queryInterface, Sequelize) => {
return queryInterface.renameColumn('Users', 'afterName', 'beforeName');
}
};
Upvotes: 7