javascript - 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
似乎存在于数据库中。
[编辑2]
在手动执行命令后,
ALTER TABLE "installation_sites" DROP CONSTRAINT "installation_sites_contractor_id_fkey"
最初它找不到约束,但我再次运行,答案是found null row
,约束消失了。我再次运行我的服务器,并创建了几个重复的 installation_sites_contractor_id_fkey 密钥,然后 sequelize 开始抱怨其他 fkey。我仍然无法理解这种行为
解决方案
推荐阅读
- java - 检查 MySQL 表是否包含值?
- php - PHP Curl Web Scraping 不返回 HTML
- mongodb - 如果字段存在必须为真,但如果不存在则必须像真一样通过
- python - 尝试在 PyQt QLabel 上绘制矩形时图像未显示
- xaml - 如何获取 UWP XAML 页面以检测何时按下 % 符号
- java - 关于过滤 Jsoup 元素的 IntelliJ 警告
- c - GStreamer:通过UDP将字符串发送到另一个管道
- r - 如何在 R 上将患病人群细分为性别 - 微生物组
- c# - 如何在 C# 中为 SDL2 加载 DLL?(DLLImport 的问题)
- reactjs - Axios如何在componentDidMount中异步获取数据