Scala,Quill,Postgres - 由于"ON CONFLICT"错误而无法插入数据库



我有一个简单的模式,在Postgres中创建表:

CREATE TABLE IF NOT EXISTS test.table
(
id     varchar(255)   NOT NULL,
action varchar(255)   NOT NULL,
text   varchar(255)   NOT NULL,

PRIMARY KEY (id, action),
CONSTRAINT fk_other FOREIGN KEY (id, action) REFERENCES test.others(action, id) ON DELETE CASCADE
);

和一个插入数据的scala代码:

testTable
.insert(lift(entity))
.onConflictUpdate(_.action, _.id)(
(t, e) => t.text -> e.text
.returning[TestRow](r => r)

但是当我运行它时,我得到了一个错误:Message, there is no unique or exclusion constraint matching the ON CONFLICT specification

我认为我在这里做的一切都很好,但是这个错误似乎很奇怪。你能帮我吗?

您的INSERT INTO ... ON CONFLICT语句缺少unique or exclusion constraint,这需要确定插入的行如何与表中的现有行发生冲突。

这可能是由于自动推理没有找到约束,或者更有可能的情况是您忘记指定标识约束的一个或多个列。

在SQL中,您可以像这样显式地指定约束:

INSERT INTO distributors (did, dname)
VALUES (9, 'Antwerp Design')
ON CONFLICT ON CONSTRAINT distributors_pkey
DO NOTHING;

最新更新