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 CFD52D1B211 for ; Sun, 6 Jun 2004 05:47:58 -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 77148-10 for ; Sun, 6 Jun 2004 08:47:52 +0000 (GMT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id B9A50D1B20F for ; Sun, 6 Jun 2004 05:47:49 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id i568lnDW030705 for ; Sun, 6 Jun 2004 08:47:49 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id i568JmVg028013 for pgsql-sql@postgresql.org; Sun, 6 Jun 2004 08:19:48 GMT From: nzanella@cs.mun.ca (Neil Zanella) X-Newsgroups: comp.databases.postgresql.sql Subject: SQL DDL: FOREIGN KEY construct and field mapping: unexpected behavior Date: 6 Jun 2004 01:19:50 -0700 Organization: http://groups.google.com Lines: 68 Message-ID: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: groups-abuse@google.com To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=LINES_OF_YELLING, LINES_OF_YELLING_2 X-Spam-Level: X-Archive-Number: 200406/45 X-Sequence-Number: 17990 Hello, Consider the following code run under PostgreSQL 7.3.4: CREATE TABLE X ( A INT, B INT, PRIMARY KEY (A, B) ); CREATE TABLE Y ( A INT, B INT, C INT, PRIMARY KEY (C), FOREIGN KEY (B, A) REFERENCES X ); INSERT INTO X (A, B) VALUES (1, 2); INSERT INTO Y (A, B, C) VALUES (1, 2, 3); The second insert causes the database server to report the following error: ERROR: $1 referential integrity violation - key referenced from y not found in x Upon examining the interpretation of PostgreSQL's REFERENCES clause I find the following: foodb=# \d x Table "public.x" Column | Type | Modifiers --------+---------+----------- a | integer | not null b | integer | not null Indexes: x_pkey primary key btree (a, b) foodb=# \d y Table "public.y" Column | Type | Modifiers --------+---------+----------- a | integer | b | integer | c | integer | not null Indexes: y_pkey primary key btree (c) Foreign Key constraints: $1 FOREIGN KEY (b, a) REFERENCES x(a, b) ON UPDATE NO ACTION ON DELETE NO ACTION It is as though PostgreSQL, instead of matching names, associated field A in table Y with field B in table X and field B in table Y with field A in table X whereas I was expecting the database server to match the names as in: field A in table Y with field A in table X and field B in table Y with field B in table X I wonder what the SQL standard has to say on this one and how the REFERENCES clause with no field names on the right hand side really works in spite of the unexpected results produced by this very simple example... Thanks, Neil