Node.js 更改列名Sequilize

Node.js 更改列名Sequilize,node.js,sequelize.js,Node.js,Sequelize.js,当我使用NodeJS和Sequelize连接访问数据库时,我想更改来自Mysql数据库的最终输出中的默认列名。 我正在使用以下代码进行连接:- import Sequelize from "sequelize"; let dbConnection = new Sequelize('DB', 'abc', 'password', { host: '127.0.0.1', port: 4242, dialect: 'mysql',

当我使用NodeJS和Sequelize连接访问数据库时,我想更改来自Mysql数据库的最终输出中的默认列名。 我正在使用以下代码进行连接:-

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 });
例如,我只想在最终的sequelize输出中,通过上述列中的tour_名称更改tour_标题。我不想更改数据库列名。

仅在最终输出中更改列名

更改数据库中的列名的步骤

这可能对您有所帮助,请遵循以下步骤

步骤1:npm安装-g sequelize cli 安装sequelize命令行工具的步骤

步骤2:续集迁移:创建-name重命名-column-tour\u title-to-tour\u name-in-tabelname

步骤3:打开新创建的迁移文件并添加以下代码

'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');
  }
};
步骤4:在命令行中运行以下命令

sequelize db:migrate
现在您的列名已成功更改。

仅在最终输出中更改列名

更改数据库中的列名的步骤

这可能对您有所帮助,请遵循以下步骤

步骤1:npm安装-g sequelize cli 安装sequelize命令行工具的步骤

步骤2:续集迁移:创建-name重命名-column-tour\u title-to-tour\u name-in-tabelname

步骤3:打开新创建的迁移文件并添加以下代码

'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');
  }
};
步骤4:在命令行中运行以下命令

sequelize db:migrate
现在您的列名已成功更改。

如果您想在输出中为列名指定别名,可以执行以下操作:

TableName.findAll({
  attributes: ['id', ['tour_title', 'tour_name']] //id, tour_title AS tour_name
})
.then(function(data) {
  res.json(data);
});
无论何时获取任何记录,都可以在级联列表中写入列名并生成别名。 检查此处的文档:

如果要在输出中为列名指定别名,可以执行以下操作:

TableName.findAll({
  attributes: ['id', ['tour_title', 'tour_name']] //id, tour_title AS tour_name
})
.then(function(data) {
  res.json(data);
});
无论何时获取任何记录,都可以在级联列表中写入列名并生成别名。
检查此处的文档:

以上答案仅适用于查询结果。如果要在数据库中保留下划线名称约定,并在javascript环境中使用例如camelCase,则需要执行以下操作:

const ChildTable = dbConnection.define('ChildTable', { //this table name is how you call it on sequelize
  tourTitle: { //this is the name that you will use on javascript
      field: 'tour_title', // this will be the name that you fetch on your db
      type: Sequelize.STRING,
  },
  parentTableId: { // You need to define the field on the model, otherwise on sequelize you'll have to use the db name of the field
      field: 'parent_table_id', //this is defined above on the association part
      type: Sequelize.INTEGER,
  }
}, { freezeTableName: true, timestamps: false, tableName: 'TableName', //this is the real name of the table on the db
underscored: true, });

ChildTable.associate = (models) => {
  ChildTable.belongsTo(models.ParentTable, {   as: 'ParentTable',   foreignKey: 'parent_table_id', onDelete: 'cascade' });
};

这样,在所有Sequelize/javascript环境中,您的字段都有一个别名,并将您的名字保留在数据库中。另请参见,您必须对关联上的外键执行相同的操作,否则您必须使用原始名称。

以上答案仅适用于查询结果。如果要在数据库中保留下划线名称约定,并在javascript环境中使用例如camelCase,则需要执行以下操作:

const ChildTable = dbConnection.define('ChildTable', { //this table name is how you call it on sequelize
  tourTitle: { //this is the name that you will use on javascript
      field: 'tour_title', // this will be the name that you fetch on your db
      type: Sequelize.STRING,
  },
  parentTableId: { // You need to define the field on the model, otherwise on sequelize you'll have to use the db name of the field
      field: 'parent_table_id', //this is defined above on the association part
      type: Sequelize.INTEGER,
  }
}, { freezeTableName: true, timestamps: false, tableName: 'TableName', //this is the real name of the table on the db
underscored: true, });

ChildTable.associate = (models) => {
  ChildTable.belongsTo(models.ParentTable, {   as: 'ParentTable',   foreignKey: 'parent_table_id', onDelete: 'cascade' });
};

这样,在所有Sequelize/javascript环境中,您的字段都有一个别名,并将您的名字保留在数据库中。另请参见,必须对关联上的外键执行相同的操作,否则必须使用原始名称。

要在输出中使用别名还是要更改数据库中的列名?不,我不想更改数据库中的名称。你可以说我想要一个别名你是在使用find或其他相关方法来获取记录吗?你是想在输出中使用别名还是想更改数据库中的列名?不,我不想更改数据库中的名称。你可以说我想要一个别名。你是在使用find或其他相关方法获取记录吗?感谢@Deepak提供了一个很好的解决方案,但这改变了数据库中的列名。但我只想在最终输出中更新名称。感谢@Deepak提供了一个好的解决方案,但这改变了数据库中的列名。但我只希望在最终输出中有一个更新后的名称。@阿披实卡加瓦尔您是否交叉检查了数据库中是否有上面列出的所有字段?列名应与上面代码中提到的相同。@阿披实卡加瓦尔数据库表中是否有列id?如果没有,则不应将其写入属性数组。可以写入表中所有列的名称。从数组中删除id可以解决此问题。@AbhishekAgarwal是否交叉检查数据库中是否有上述所有字段?列名应与上述代码中提到的相同。@AbhishekAgarwal数据库表中是否有列id?如果没有,则不应将其写入属性数组。可以写入表中所有列的名称。从数组中删除id将解决此问题。如何在关联上指定字段?我的列有下划线,但关联正在创建camelCase列名称。如何在关联上指定字段?我的列有下划线,但关联正在创建camelCase列名称。