Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V1Huh-0007A9-AF for pgsql-jdbc@arkaria.postgresql.org; Mon, 22 Jul 2013 15:20:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V1Hug-0003Hf-Mg for pgsql-jdbc@arkaria.postgresql.org; Mon, 22 Jul 2013 15:20:38 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V1Huf-0003HZ-Ul for pgsql-jdbc@postgresql.org; Mon, 22 Jul 2013 15:20:38 +0000 Received: from mo1.mail-out.ovh.net ([178.32.228.1]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V1Huc-0005FO-8g for pgsql-jdbc@postgresql.org; Mon, 22 Jul 2013 15:20:37 +0000 Received: from mail184.ha.ovh.net (b9.ovh.net [213.186.33.59]) by mo1.mail-out.ovh.net (Postfix) with SMTP id 2B7021004049 for ; Mon, 22 Jul 2013 17:20:32 +0200 (CEST) Received: from b0.ovh.net (HELO queueout) (213.186.33.50) by b0.ovh.net with SMTP; 22 Jul 2013 17:20:31 +0200 Received: from 89-156-198-77.rev.numericable.fr (HELO ?192.168.28.52?) (sylvain%ilm-informatique.fr@89.156.198.77) by ns0.ovh.net with SMTP; 22 Jul 2013 17:20:29 +0200 Message-ID: <51ED4DBC.7020307@ilm-informatique.fr> Date: Mon, 22 Jul 2013 17:20:28 +0200 From: Sylvain Cuaz User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org X-Ovh-Mailout: 178.32.228.1 (mo1.mail-out.ovh.net) Subject: DatabaseMetaData getImportedKeys() order Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Ovh-Tracer-Id: 5601633512011673458 X-Ovh-Remote: 89.156.198.77 (89-156-198-77.rev.numericable.fr) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-OVH-SPAMSTATE: OK X-OVH-SPAMSCORE: 0 X-OVH-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrfeeijedrfedvucetufdoteggodetrfcurfhrohhfihhlvgemucfqggfjnecuuegrihhlohhuthemuceftddtnecu X-Spam-Check: DONE|U 0.5/N X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: 0 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrfeeijedrfedvucetufdoteggodetrfcurfhrohhfihhlvgemucfqggfjnecuuegrihhlohhuthemuceftddtnecu X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org Hi, In org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData both getImportedKeys() and getExportedKeys() call getImportedExportedKeys() which order its result strictly according to java.sql.DatabaseMetaData : PKTABLE_CAT, PKTABLE_SCHEM, PKTABLE_NAME, KEY_SEQ. But this is not enough when 2 multi-field keys point to the same table. E.g. : CREATE TABLE test."PERSON" ( "FIRST_NAME" character varying(100) NOT NULL, "LAST_NAME" character varying(100) NOT NULL, "FIRST_NAME_PARENT_1" character varying(100), "LAST_NAME_PARENT_1" character varying(100), "FIRST_NAME_PARENT_2" character varying(100), "LAST_NAME_PARENT_2" character varying(100), CONSTRAINT "PERSON_pkey" PRIMARY KEY ("FIRST_NAME" , "LAST_NAME" ), CONSTRAINT "PARENT_1_fkey" FOREIGN KEY ("FIRST_NAME_PARENT_1", "LAST_NAME_PARENT_1") REFERENCES test."PERSON" ("FIRST_NAME", "LAST_NAME") MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT "PARENT_2_fkey" FOREIGN KEY ("FIRST_NAME_PARENT_2", "LAST_NAME_PARENT_2") REFERENCES test."PERSON" ("FIRST_NAME", "LAST_NAME") MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE ); For this method to be useful the constraint must be used otherwise we can't know which first name goes with which last name. The H2 database (and I believe MySQL as well) order by PKTABLE_CAT, PKTABLE_SCHEM, PKTABLE_NAME, FK_NAME, KEY_SEQ (see https://code.google.com/p/h2database/source/browse/tags/version-1.3.172/h2/src/main/org/h2/jdbc/JdbcDatabaseMetaData.java ). For 9.2-1003, con.conname just needs to be added before pos.n at line 3440 : --- AbstractJdbc2DatabaseMetaData-9.2-1003.java 2013-07-22 17:07:25.001273200 +0200 +++ AbstractJdbc2DatabaseMetaData-multiField.java 2013-07-22 17:09:16.570654600 +0200 @@ -3437,13 +3437,16 @@ sql += " AND fkc.relname = " + escapeQuotes(foreignTable); } + // con.conname required otherwise we cannot assemble + // multi-field foreign keys when a table has multiple links + // to another if (primaryTable != null) { - sql += " ORDER BY fkn.nspname,fkc.relname,pos.n"; + sql += " ORDER BY fkn.nspname,fkc.relname,con.conname,pos.n"; } else { - sql += " ORDER BY pkn.nspname,pkc.relname,pos.n"; + sql += " ORDER BY pkn.nspname,pkc.relname,con.conname,pos.n"; } return createMetaDataStatement().executeQuery(sql); Cheers, Sylvain -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc