Received: from post.webmailer.de (natwar.webmailer.de [192.67.198.70]) by postgresql.org (8.11.3/8.11.4) with ESMTP id fB82pZm27354 for ; Fri, 7 Dec 2001 21:51:35 -0500 (EST) (envelope-from barwick@gmx.net) Received: from there (pD9EB1434.dip.t-dialin.net [217.235.20.52]) by post.webmailer.de (8.9.3/8.8.7) with SMTP id DAA20030; Sat, 8 Dec 2001 03:46:52 +0100 (MET) Message-Id: <200112080246.DAA20030@post.webmailer.de> Content-Type: text/plain; charset="iso-8859-1" From: Ian Barwick To: Phill Kenoyer , pgsql-admin@postgresql.org Subject: Re: Primary Key Problems Date: Sat, 8 Dec 2001 02:52:55 +0100 X-Mailer: KMail [version 1.3.1] References: <20011208014433.GA2913@c0de.net> In-Reply-To: <20011208014433.GA2913@c0de.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Archive-Number: 200112/56 X-Sequence-Number: 2780 On Saturday 08 December 2001 02:44, Phill Kenoyer wrote: > Here is a good one. I have three fields set for my primary key. Now I > thought that a primary key was unique, and dups can not be inserted. That's the theory ;-) > CREATE TABLE "inventory" ( > "stock" character varying(50) NOT NULL, > "inventory_type" character varying(20) DEFAULT 'unknown' NOT NULL, > "client_id" integer NOT NULL, > [...] > Constraint "inventory_pkey" > Primary Key ("stock", "inventory_type", "client_id") > ); > > and I insert into the db and get dups. > > client_id | stock | inventory_type > -----------+-------+---------------- > 81 | 2001 | new > 81 | 2001 | new > 81 | 2003 | new > 81 | 2005 | new > 81 | 2006 | new > 81 | 2006 | new > 81 | 2008 | new > 81 | 2008 | new > > Why did I get dups in my data? Are the duplicated values in "stock" and "inventory_type" genuinely identical? As they are both VARCHARs they could contain differing amounts of trailing spaces or other 'invisible' characters which would not be obvious from the console, but would represent valid and unique values to the constraint checker. These could result from unfiltered input, such as from an HTML form. Also, does the primary key still exist? SELECT relname, relowner FROM pg_class WHERE relname='inventory_pkey' Ian Barwick