how to find foreign key details (column, that is)

Поиск
Список
Период
Сортировка
От Karsten Hilbert
Тема how to find foreign key details (column, that is)
Дата
Msg-id 20081214184124.GA8624@merkur.hilbert.loc
обсуждение исходный текст
Ответы Re: how to find foreign key details (column, that is)  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
Hello all,

suppose I know that there are several tables with foreign
keys pointing to

    demographics.identity.pk

With the help of pg_constraint I can get a list of *tables*
which hold those foreign keys (schema = demographics, tbl = identity,
col = pk):


select
    %(schema)s as referenced_schema,
    %(tbl)s as referenced_table,
    %(col)s as referenced_column,
    conrelid::regclass as foreign_table,
    confkey as referenced_column_list
from
    pg_constraint
where
    contype = 'f'
        and
    confrelid = (
        select oid from pg_class where relname = %(tbl)s and relnamespace = (
            select oid from pg_namespace where nspname = %(schema)s
         )
    )    and
    (
        select attnum
        from pg_attribute
        where
            attrelid = (select oid from pg_class where relname = %(tbl)s and relnamespace = (
                select oid from pg_namespace where nspname = %(schema)s
            ))
                and
            attname = %(col)s
    ) = any(confkey)


I cannot, however, for the life of it find out how to learn
the *column* the foreign key is on. IOW I can find out that

    clinical.allergy

has a foreign key to

    demographics.identity.pk

but I cannot find out that the column representing the
foreign key is

    clinical.allergy.fk_identity

How can I go about this ?

Thanks,
Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346

В списке pgsql-general по дате отправления:

Предыдущее
От: Bob Gobeille
Дата:
Сообщение: log rotate and open connections
Следующее
От: Ivan Sergio Borgonovo
Дата:
Сообщение: from 2 keys to serial