X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 06B1CD1B20F for ; Tue, 8 Jun 2004 02:53:09 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 41768-10 for ; Tue, 8 Jun 2004 05:53:09 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id ED100D1B241 for ; Tue, 8 Jun 2004 02:53:06 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i585r2xM026678; Tue, 8 Jun 2004 01:53:02 -0400 (EDT) To: Kyle Cc: pgsql-sql@postgresql.org, Peter Eisentraut Subject: Re: Getting FK relationships from information_schema In-reply-to: <40C500D8.9060009@actarg.com> References: <40C500D8.9060009@actarg.com> Comments: In-reply-to Kyle message dated "Mon, 07 Jun 2004 17:57:12 -0600" Date: Tue, 08 Jun 2004 01:53:02 -0400 Message-ID: <26677.1086673982@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/58 X-Sequence-Number: 18003 Kyle writes: > I'm trying to get my application to deduce foreign key relationships > automatically so it can perform appropriate joins for the user. I'm new > to information_schema and having problems getting what I want. > ... > I can determine all the primary key fields nicely, and I can tell what > fields are foreign keys. The problem is, I can't determine where the > foreign keys are pointing. The problem is, the constraint names ($1, > $2, etc.) are not unique so I don't know how to join the third query > into the fourth. Hmm, this is messy :-(. The SQL spec requires constraint names to be unique within a schema. Postgres doesn't require them to be unique even within a table. We were aware that there were some compatibility issues there, but I hadn't realized that the information_schema design is fundamentally dependent on the assumption of schema-wide uniqueness for these names. For a number of reasons (backwards compatibility being the hardest to argue with), adopting the spec's restriction on constraint names seems unlikely to happen. You could of course follow it within your own database designs, but I don't foresee Postgres enforcing it on everyone. In the short run I think your only answer is to dig deeper than information_schema and look directly at the Postgres catalogs. In the long run it'd be nice to have a cleaner answer, but I'm not sure what it ought to look like. Can we get away with adding implementation-specific columns to information_schema tables? If not, what other alternatives are there? regards, tom lane