From 0fc654981a86f5f0c41d5727f93907e7895dc140 Mon Sep 17 00:00:00 2001 From: David Binney Date: Tue, 8 Mar 2016 16:58:17 +1000 Subject: [PATCH] gh #8160 : @markstory, Updating query to allow for multi column constraints and composite keys --- src/Database/Schema/PostgresSchema.php | 40 ++++++++++++-------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Database/Schema/PostgresSchema.php b/src/Database/Schema/PostgresSchema.php index f8f1a5b8b37..e271a127e33 100644 --- a/src/Database/Schema/PostgresSchema.php +++ b/src/Database/Schema/PostgresSchema.php @@ -191,27 +191,25 @@ protected function _defaultValue($default) */ public function describeIndexSql($tableName, $config) { - $sql = 'SELECT - c2.relname, - a.attname, - i.indisprimary, - i.indisunique - FROM pg_catalog.pg_class AS c, - pg_catalog.pg_class AS c2, - pg_catalog.pg_index AS i, - pg_catalog.pg_attribute AS a - WHERE c.oid = ( - SELECT c.oid - FROM pg_catalog.pg_class c - LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace - WHERE c.relname = ? - AND n.nspname = ? - ) - AND c.oid = i.indrelid - AND c.oid = a.attrelid - AND a.attnum = ANY(i.indkey) - AND i.indexrelid = c2.oid - ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname, a.attnum'; + $sql = "SELECT + c.conname AS name, + c.contype AS type, + a.attname AS column_name, + c.confmatchtype AS match_type, + c.confupdtype AS on_update, + c.confdeltype AS on_delete, + c.confrelid::regclass AS references_table, + ab.attname AS references_field + FROM pg_catalog.pg_namespace n + INNER JOIN pg_catalog.pg_constraint c ON (n.oid = c.connamespace) + INNER JOIN pg_catalog.pg_class cl ON (cl.oid = c.conrelid) + INNER JOIN pg_catalog.pg_attribute a ON (a.attrelid = cl.oid AND c.conrelid::regclass = a.attrelid::regclass) + INNER JOIN pg_catalog.pg_attribute ab ON (ab.attrelid = cl.oid AND a.attrelid = ab.attrelid AND a.attnum = ab.attnum) + WHERE n.nspname = ? + AND a.attnum = any(c.conkey) + AND c.conrelid = ?::regclass + AND c.contype='f' + ORDER BY c.conname, a.attnum"; $schema = 'public'; if (!empty($config['schema'])) {