這怎麼可能發生?使用SELECT NOT EXISTS INSERT時出錯重複鍵
IntegrityError: (IntegrityError) duplicate key value violates unique constraint "r_u_pkey"
DETAIL: Key (r_id, u_id)=(2660, 10182) already exists.
'INSERT INTO r_u(r_id, u_id) SELECT %s, %s WHERE NOT EXISTS (
SELECT 1 FROM r_u WHERE r_id = %s AND u_id = %s
)' (2660, 10182, 2660, 10182)
上有(r_id, u_id)
主鍵:
CREATE TABLE r_u
(
r_id integer NOT NULL,
u_id integer NOT NULL,
CONSTRAINT r_u_pkey PRIMARY KEY (r_id, u_id)
)
服務器是9.3.5的Postgres和連接已自動提交的。
http://sqlfiddle.com/#!15/5ccfd/5 - 例如@ SqlFiddle – 2015-02-09 14:25:58