小编mar*_*hon的帖子

如何使 ON CONFLICT 适用于复合外键列?

我无法ON CONFLICT处理外键为复合的外键列。这是一个例子。

create table foreign_table (
   id_a text    not null,
   id_b text    not null,
   id   integer primary key,
   constraint ft_a_b_key unique (id_a, id_b)
);

create table my_table (
   id          integer,
   ftable_id_a text,
   ftable_id_b text,
   constraint my_table_a_b_fk
      foreign key (ftable_id_a, ftable_id_b) references foreign_table (id_a, id_b)
);
Run Code Online (Sandbox Code Playgroud)

使用此查询:

insert into tcell_test.my_table (id, ftable_id_a, ftable_id_b) 
    values (3, 'a3', 'b3') on conflict do nothing ;
Run Code Online (Sandbox Code Playgroud)

比如说,'a3'不在 中foreign_table,我希望ON CONFLICT能够处理该错误。

相反,我收到错误:

[23503] ERROR: insert or update on table …
Run Code Online (Sandbox Code Playgroud)

postgresql foreign-key exception

5
推荐指数
1
解决办法
9357
查看次数

标签 统计

exception ×1

foreign-key ×1

postgresql ×1