README.md edited online with Bitbucket
[m6w6/pq-gateway] / lib / pq / Gateway / Table / Relations.php
index ab74d4060b23bdd49b7d795aa8b29101b2486cb7..3e4d7fbd8280a622d352ff1e9e607c3b3fe74fc9 100644 (file)
@@ -6,11 +6,15 @@ use \pq\Gateway\Table;
 
 const RELATION_SQL = <<<SQL
 select
-        substring(att1.attname from '^.*(?=_'||att2.attname||'$)') as "id"
-       ,cl1.relname                                                as "foreignTable"
-    ,att1.attname                                               as "foreignColumn"
-       ,cl2.relname                                                as "referencedTable"
-    ,att2.attname                                               as "referencedColumn"
+        case att1.attname
+        when att2.attname
+               then att1.attname
+               else substring(att1.attname from '^.*(?=_'||att2.attname||'$)')
+        end          as "id"
+       ,cl1.relname  as "foreignTable"
+       ,att1.attname as "foreignColumn"
+       ,cl2.relname  as "referencedTable"
+       ,att2.attname as "referencedColumn"
 from
      pg_constraint co
     ,pg_class      cl1
@@ -46,7 +50,7 @@ class Relations
                        $table->getQueryExecutor()->execute(
                                new \pq\Query\Writer(RELATION_SQL, array($table->getName())),
                                function($result) use($table, $cache) {
-                                       $this->references = $result->map(array(0,1), array(2,3,4), \pq\Result::FETCH_OBJECT);
+                                       $this->references = $result->map(array(0,1), array(1,2,3,4), \pq\Result::FETCH_OBJECT);
                                        $cache->set("$table#relations", $this->references);
                                }
                        );