如何删除 sequelize v6 中自动创建的列 ID?

How to remove auto created column id in sequelize v6?

我正在尝试建立一个连接到我的 postgres 数据库中的 VIEW 的模型,以便我可以从中读取数据,但是每次我尝试从视图中获取数据时,我都会收到一个错误,显示列 'id' 不存在。 Sequelize 在创建模型时自动创建一个 id 整数列作为主键,但由于我的视图没有 id 列,因此它会抛出错误。我已经了解了如何使用 sequelize.define() 删除 id 列。但是我使用的是基于 class 的模型,我不知道如何以这种方式删除属性。非常感谢任何想法!谢谢!。使用续集版本 6

我尝试在底部使用 removeAttr,但没有用。

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Prjt_source_percent_each extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  Prjt_source_percent_each.init({
    project_id: DataTypes.STRING,
    phase: DataTypes.STRING,
    commodity: DataTypes.STRING,
    comm_type: DataTypes.STRING,
    source_energy_baseline: DataTypes.REAL,
    source_energy_savings: DataTypes.REAL,
    savings_percent: DataTypes.DOUBLE
  }, {
    sequelize,
    modelName: 'Prjt_source_percent_each',
    tableName: 'prjt_source_percent_each',
    removeAttr: 'id'
  });
  return Prjt_source_percent_each;
};

你可以尝试使用:

Prjt_source_percent_each.removeAttribute('id');

在返回模型定义之前?更多详情:https://sequelize.org/master/class/lib/model.js~Model.html#static-method-removeAttribute

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Prjt_source_percent_each extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  Prjt_source_percent_each.init({
    project_id: DataTypes.STRING,
    phase: DataTypes.STRING,
    commodity: DataTypes.STRING,
    comm_type: DataTypes.STRING,
    source_energy_baseline: DataTypes.REAL,
    source_energy_savings: DataTypes.REAL,
    savings_percent: DataTypes.DOUBLE
  }, {
    sequelize,
    modelName: 'Prjt_source_percent_each',
    tableName: 'prjt_source_percent_each',
  });
  // remove the attribute
  // https://sequelize.org/master/class/lib/model.js~Model.html#static-method-removeAttribute
  Prjt_source_percent_each.removeAttribute('id');
  return Prjt_source_percent_each;
};