Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 67D3C9FB43A for ; Tue, 20 Mar 2007 06:32:52 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 22957-01 for ; Tue, 20 Mar 2007 06:32:44 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by postgresql.org (Postfix) with ESMTP id 1F48D9FB529 for ; Tue, 20 Mar 2007 06:27:48 -0300 (ADT) Received: from server3.office.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 293B077398; Tue, 20 Mar 2007 09:27:43 +0000 (GMT) Received: from [192.168.1.36] (dell36.office.archonet.com [192.168.1.36]) by server3.office.archonet.com (Postfix) with ESMTP id DD53B27401A; Tue, 20 Mar 2007 09:27:42 +0000 (GMT) Message-ID: <45FFA90E.1020405@archonet.com> Date: Tue, 20 Mar 2007 09:27:42 +0000 From: Richard Huxton User-Agent: IceDove 1.5.0.9 (X11/20061220) MIME-Version: 1.0 To: remco lengers Cc: pgsql-sql@postgresql.org Subject: Re: ERROR: duplicate key violates unique constraint References: <32207abd0703200211l46acdab7p5a2d68d69ffc5b6c@mail.gmail.com> In-Reply-To: <32207abd0703200211l46acdab7p5a2d68d69ffc5b6c@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.054 tagged_above=0 required=5 tests=AWL, BAYES_50, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200703/269 X-Sequence-Number: 27907 remco lengers wrote: > Hi List, > > Its been a while since I worked with databases and I am running into the > following which I have not been able to find the root cause for: > > I have the follow SQL statement: > > INSERT INTO reference VALUES (DEFAULT,'123','2',1); > > Which generates the following error: > > "ERROR: duplicate key violates unique constraint "reference_pkey"" > > And the table definition looks like: > > CREATE TABLE reference ( > referencelist_nr serial unique, > reference_text varchar(40) NOT NULL, > reference_type integer NOT NULL, > Topic_Id integer NOT NULL, > PRIMARY KEY (referencelist_nr), > FOREIGN KEY (Topic_Id) REFERENCES Topic (Topic_Id), > FOREIGN KEY (reference_type) REFERENCES reference_type (reference_type_nr) > ); > > It seems to me for some reason "DEFAULT" doesn't select the next SERIAL. It does, but if you've been manually setting that value then the SERIAL doesn't know about it. Sequences are fast and light, but they can be so because they don't have to check the table. > I have been adding data with "COPY" into this table....does that break > something? There you go. After a bulk load you'll want to run something like: SELECT setval('reference_referencelist_nr_seq', (SELECT max(referencelist_nr)+1 FROM reference)); Have a look at pg_dump to see how it does this sort of thing. -- Richard Huxton Archonet Ltd