首页 > 解决方案 > UnhandledPromiseRejectionWarning: SequelizeUnknownConstraintError: 未知约束错误

问题描述

我正在使用 NodeJs 和 Express 中的 API 来使用 Sequelize 连接 PostgreSQL 数据库。当我在终端的 Sequelize 输出末尾运行我的程序时,我得到:

Executing (default): ALTER TABLE "installation_sites" DROP CONSTRAINT "installation_sites_contractor_id_fkey"
(node:21312) UnhandledPromiseRejectionWarning: SequelizeUnknownConstraintError: Unknown constraint error
    at Query.formatError (C:\Users\caleb\OneDrive\Documentos\Aplicativo_RDO\API\node_modules\sequelize\lib\dialects\
postgres\query.js:376:17)
    at Query.run (C:\Users\caleb\OneDrive\Documentos\Aplicativo_RDO\API\node_modules\sequelize\lib\dialects\
postgres\query.js:87:18)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
Executing (default): ALTER TABLE "installation_sites" ALTER COLUMN "contractor_id" SET NOT NULL;
ALTER TABLE "installation_sites"  ADD FOREIGN KEY ("contractor_id") REFERENCES "contractors" ("contractor_id") 
ON DELETE NO ACTION ON UPDATE CASCADE;
(node:21312) UnhandledPromiseRejectionWarning: SequelizeUnknownConstraintError: Unknown constraint error
    at Query.formatError (C:\Users\caleb\OneDrive\Documentos\Aplicativo_RDO\API\node_modules\sequelize\lib\
dialects\postgres\query.js:376:17)
    at Query.run (C:\Users\caleb\OneDrive\Documentos\Aplicativo_RDO\API\node_modules\sequelize\lib\
dialects\postgres\query.js:87:18)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:21312) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
This error originated either by throwing inside of an async function without a catch block, 
or by rejecting a promise which was not handled with .catch(). To terminate the node process 
on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` 
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21312) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. 
In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我不明白是什么原因造成的。这是我参与的模型:

InstallationSite.js 模型:

const Sequelize = require("sequelize");
const sequelize = require("../database/index");
const Contractor = require("../models/Contractor");
const JobPosition = require("./JobPosition");

class InstallationSite extends Sequelize.Model {}

InstallationSite.init(
  {
    installation_site_id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
    },
    installation_site_status: {
      type:Sequelize.STRING,
      allowNull:false,
      validate:{
        isIn:[['Em Andamento', "Finalizada", "Não Iniciada"]]
      }
    },
    enterprise_name: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    enterprise_start_date: {
      type: Sequelize.DATEONLY,
      allowNull: false,
    },
    enterprise_duration_days: {
      type: Sequelize.INTEGER,
      allowNull: false,
    },
    enterprise_city: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    enterprise_state: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    contractor_id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: "contractors",
        key: "contractor_id",
      },
    },
  },
  {
    sequelize,
    timestamps: false,
    tableName: "installation_sites",
  }
);

InstallationSite.belongsTo(Contractor, {
  foreignKey: "contractor_id",
  targetKey: "contractor_id",
});
Contractor.hasMany(InstallationSite, {
  foreignKey: "contractor_id",
  sourceKey: "contractor_id",
});

InstallationSite.belongsToMany(JobPosition, {
  through: "contracts",
  foreignKey: "installation_sites_intallation_site_id",
  as: "positions",
});

JobPosition.belongsToMany(InstallationSite, {
  through: "contracts",
  foreignKey: "job_positions_job_position_id",
  as: "installations",
});

sequelize.sync({ alter: true });
module.exports = InstallationSite;

Contractor.js 模型:

const Sequelize = require("sequelize");
const sequelize = require("../database/index");

class Contractor extends Sequelize.Model {}

Contractor.init(
  {
    contractor_id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false,
    },
    contractor_name: {
      type: Sequelize.STRING,
      validate: {
        is: {
          args: /[a-zA-z]/,
          msg: "Contractor name must contain only letters",
        },
        len: {
          args: [5, 50],
          msg:
            "Contractor name must contain at least five letters and less than 50 letters",
        },
        notEmpty: {
          args: true,
          msg: "Contractor name is not optional",
        },
      },
    },
  },
  {
    sequelize,
    timestamps: false,
    tableName: "contractors",
  }
);

sequelize.sync({ alter: true });
module.exports = Contractor;

我想知道是否有人可以帮助我找出导致此警告的原因。

[编辑] 约束installation_sites_contractor_id_fkey似乎存在于数据库中。

DB内容图片

[编辑2]

在手动执行命令后, ALTER TABLE "installation_sites" DROP CONSTRAINT "installation_sites_contractor_id_fkey"最初它找不到约束,但我再次运行,答案是found null row,约束消失了。我再次运行我的服务器,并创建了几个重复的 installation_sites_contractor_id_fkey 密钥,然后 sequelize 开始抱怨其他 fkey。我仍然无法理解这种行为

标签: javascriptnode.jspostgresqlsequelize.js

解决方案


推荐阅读