Quantcast
Channel: Question and Answer » postgresql
Viewing all articles
Browse latest Browse all 1138

How to list all tables associated with specific foreign key?

$
0
0

I have this Postgresql query:

select (select r.relname from pg_class r where r.oid = c.conrelid) as table, 
    (select r.relkind from pg_class r where r.oid = c.conrelid) as type,
       (select array_agg(attname) from pg_attribute 
        where attrelid = c.conrelid and ARRAY[attnum] <@ c.conkey) as col, 
       (select r.relname from pg_class r  where r.oid = c.confrelid) as ftable,
from pg_constraint c 
where c.confrelid = (select oid from pg_class where relname = 'table name') and 
      c.confkey @> (select array_agg(attnum) from pg_attribute 
                    where attname = 'column name' and attrelid = c.confrelid)

which for a specific fk from a specific table it will present the other tables that associated to this key. This query is working fine…

My goal is to 2 things:

  1. add a column which contains the schema of the tables
  2. this query present only tables… how do i add views? – SOLVED

I know it’s something simple probably with pg_class but i don’t know how to resolve this.


Viewing all articles
Browse latest Browse all 1138

Trending Articles