如何跳过INSERT语句中引发的异常?

问题描述 投票:0回答:1

当我运行以下SQL代码时:

INSERT INTO table
(column1, column1)
SELECT column1, column2
FROM other_table
WHERE column2 = 'foo'
ON CONFLICT DO NOTHING;

我知道

PG::CheckViolation: ERROR:
new row for relation "table" violates
check constraint "tables_column1_valid_first_char"

如何跳过违反该检查约束的行?

我尝试过

INSERT INTO table
(column1, column1)
SELECT column1, column2
FROM other_table
WHERE column2 = 'foo'
ON CONFLICT  ON CONSTRAINT tables_column1_valid_first_char DO NOTHING;

但是失败了>>

PG::WrongObjectType: ERROR:
constraint in ON CONFLICT clause has no associated index

该约束如下:

ADD CONSTRAINT tables_column1_valid_first_char
  CHECK (SUBSTRING(text, 1,1) ~* '^[a-zàâçéèêëîïôûùüÿæœ]');

当我运行以下SQL代码时:INSERT INTO表(column1,column1)SELECT column1,column2 FROM other_table WHERE column2 ='foo'ON CONFLICT DO NOTHING;我得到PG :: CheckViolation:错误:新...

sql postgresql insert constraints
1个回答
0
投票

ON CONFLICT仅处理唯一约束,不适用于检查约束(或外键约束)。

© www.soinside.com 2019 - 2024. All rights reserved.