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

标签 javascript node.js orm sequelize.js

我正在尝试设置一个连接到我的 postgres 数据库中的 VIEW 的模型,以便我可以从中读取,但是每次我尝试从 View 中获取数据时,我都会收到一个错误,指出列“id”不存在。创建模型时,Sequelize 会自动创建一个 id 整数列作为主键,但由于我的 View 没有 id 列,因此会引发错误。我已经看到如何使用 sequelize.define() 删除 id 列。但是我使用的是基于类的模型,我不知道如何以这种方式删除属性。任何想法都非常感谢!谢谢!。使用 sequelize 版本 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;
};

关于javascript - 如何在 sequelize v6 中删除自动创建的列 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67894056/

相关文章:

javascript - 如何在没有警报消息(iOS)的情况下获得 webview 中的地理定位权限?

javascript - 是否可以使用javascript在点击超文本上打开文件上传窗口

javascript - 在浏览器中动态切换 XSLT 样式表?

javascript - 如何使用 localhost 在浏览器中运行 Web 应用程序?

php - Symfony2 给出空白页

javascript - jQuery 在 Wordpress 中点击显示/隐藏

node.js - Nodejs API、Docker (Swarm)、可扩展性和存储

javascript - 在 Node js 中用另一个替换字符串的所有 URL 出现

database - 使用 ORM 进行数据验证

java - Hibernate 集合保留对已删除实体的引用