我在 PostgreSQL 数据库中使用这个表:
当我尝试使用以下代码进行
create table if not exists "Service" (
_id uuid not null primary key,
service text not null,
"count" integer not null,
"date" timestamp with time zone,
team uuid,
organisation uuid,
"createdAt" timestamp with time zone not null,
"updatedAt" timestamp with time zone not null,
unique (service, "date", organisation),
foreign key ("team") references "Team"("_id"),
foreign key ("organisation") references "Organisation"("_id")
);
当我尝试使用以下代码进行
Sequelize
的upsert
时,它会抛出一个错误:
Service.upsert({ team, date, service, organisation, count }, { returning: true })
错误信息如下:
错误:违反唯一约束条件“Service_service_date_organisation_key”的重复键值
(service,date,organisation)=(xxx,2022-12-30 01:00:00+01,12345678-5f63-1bc6-3924-517713f97cc3)已经存在。
但根据Sequelize文档,它应该可以正常工作:https://sequelize.org/docs/v6/other-topics/upgrade/#modelupsert
Postgres用户请注意:如果upsert负载包含PK字段,则将使用PK作为冲突目标。否则,将选择第一个唯一约束作为冲突键。
我该如何找到这个重复键错误,并使用复合唯一键unique (service, "date", organisation)
使其正常工作?