I want to change default column name in my final output which comes from Mysql database when i am accessing database using NodeJS and Sequelize connection. I am connecting using following code:-
import Sequelize from "sequelize";
    let dbConnection = new Sequelize('DB', 'abc', 'password', {
        host: '127.0.0.1',
        port: 4242,
        dialect: 'mysql',
        dialectOptions: {
            requestTimeout: 0,
        },
    });
const Listing = dbConnection.define('TableName', {
    tour_title: {
        type: Sequelize.STRING,
    }
}, { freezeTableName: true, timestamps: false });
For example, I want to change tour_title by tour_name in the above column only in the final sequelize output. I do not want any change in database column names.
To change column name only in final output
TableName.findAll({
  attributes: ['id', ['tour_title', 'tour_name']] /
})
.then((data) => {
 console.log(data);
});
To change column name in database
This may help you, follow this steps
Step 1: npm install -g sequelize-cli
To install sequelize command line tool
Step 2: sequelize migration:create --name rename-column-tour_title-to-tour_name-in-tabelname
Step 3: Open newly created migration file and add below code
'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.renameColumn('tableName', 'tour_title', 'tour_name');
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.renameColumn('tableName', 'tour_name', 'tour_title');
  }
};
Step 4: in command line run this below command
sequelize db:migrate
Now your column name changed successfully.
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