UPSERT Postgres 9.5

时间:2016-06-02 07:47:04

标签: postgresql upsert

我在CentOS 6上使用Postgres 9.5。 我有下表(DDL)。

CREATE TABLE core_table.user_list(
  user_id SMALLSERIAL DEFAULT nextval('core_table.user_list_user_id_seq'::regclass) NOT NULL,
  user_name VARCHAR(50) NOT NULL,
  full_name VARCHAR(255),
  role_id INTEGER DEFAULT 1,
  is_accessible INTEGER DEFAULT 1,
  remarks VARCHAR(255),
  password VARCHAR(50),
  customer_schema VARCHAR(50),
  core_schema VARCHAR(50),
  CONSTRAINT user_list_pkey PRIMARY KEY(user_id, user_name),
  CONSTRAINT user_list_user_id_key UNIQUE(user_id)
) 
WITH (oids = false);

有4行现有数据。

Row 1:
user_id: 1
user_name: kelly
full_name: kelly petz
role_id: 1
is_accessible: 1
remarks: <null>
password: test123
customer_schema: kelly_db1
core_schema: kelly_db1

Row 2:
user_id: 2
user_name: kelly
full_name: kelly petz
role_id: 1
is_accessible: 1
remarks: <null>
password: test123
customer_schema: petz_db1
core_schema: kelly_db1

Row 3:
user_id: 3
user_name: sam
full_name: sam howiz
role_id: 1
is_accessible: 1
remarks: <null>
password: test456
customer_schema: kelly_db1
core_schema: kelly_db1

Row 4:
user_id: 4
user_name: jon
full_name: jon lam
role_id: 1
is_accessible: 1
remarks: <null>
password: test789
customer_schema: lam_db1
core_schema: lam_db1

当我尝试执行以下UPSERT时遇到错误。

INSERT INTO core_table.user_list 
(user_name, full_name, remarks, password, customer_schema, core_schema, role_id, is_accessible) 
VALUES 
('kelly', 'kelly petz', 'The Boss', 'test123', 'snoppy_db1', 'snoppy_db1', 1, 1) 
ON CONFLICT (user_name) 
DO UPDATE SET 
    password = 'test123', 
    remarks = 'The Boss';

错误是“错误:没有与ON CONFLICT规范匹配的唯一或排除约束”。

有人可以为我指出我的错误以及如何解决它?

2 个答案:

答案 0 :(得分:0)

如果您对user_name列没有任何约束,则不会发生冲突,on conflict (user_name)没有任何意义。

您需要user_name列上的某种unicity约束。

例如

CREATE UNIQUE INDEX user_list_name_index on  user_list (user_name);

编辑以下评论:

看起来你想要这个表模式:

CREATE TABLE core_table.user_list(
  user_id SMALLSERIAL DEFAULT nextval('core_table.user_list_user_id_seq'::regclass) NOT NULL,
  user_name VARCHAR(50) NOT NULL,
  full_name VARCHAR(255),
  role_id INTEGER DEFAULT 1,
  is_accessible INTEGER DEFAULT 1,
  remarks VARCHAR(255),
  password VARCHAR(50),
  customer_schema VARCHAR(50),
  core_schema VARCHAR(50),
  CONSTRAINT user_list_pkey PRIMARY KEY(user_id, user_name),
  CONSTRAINT user_list_user_id_key UNIQUE(user_id),
  CONSTRAINT user_list_name_schema PRIMARY KEY(user_name, customer_schema),
) 
WITH (oids = false);

然后是这个on conflict子句:

INSERT INTO core_table.user_list 
(user_name, full_name, remarks, password, customer_schema, core_schema, role_id, is_accessible) 
VALUES 
('kelly', 'kelly petz', 'The Boss', 'test123', 'snoppy_db1', 'snoppy_db1', 1, 1) 
ON CONFLICT (user_list_name_schema) 
DO UPDATE SET 
    password = 'test123', 
    remarks = 'The Boss';

答案 1 :(得分:0)

违反唯一约束时调用on conflict子句。在给定的表格中,user_name不是唯一的(user_iduser_name的组合)。

如果您想使用这样的条款,您必须以某种方式使user_name唯一,例如,通过添加约束:

ALTER TABLE user_list ADD CONSTRAINT user_name_unique UNIQUE (user_name);