rap*_*2-h 5 sql postgresql node.js sequelize.js
我在 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")
);
Run Code Online (Sandbox Code Playgroud)
当我尝试upsert使用Sequelize以下代码时,它会抛出错误:
Service.upsert({ team, date, service, organisation, count }, { returning: true })
Run Code Online (Sandbox Code Playgroud)
错误是:
错误:重复的键值违反了唯一约束“Service_service_date_organization_key”
密钥(服务、日期、组织)= (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)?
您的问题似乎与问题 #13240有关。
如果您使用的是Sequelize 6.12或更高版本,您应该能够使用以下明确列表conflictFields:
Service.upsert(
{ team, date, service, organisation, count },
{ conflictFields: ["service", "date", "organisation"] },
{ returning: true }
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2574 次 |
| 最近记录: |