Re: Res: Finding all tables that have foreign keys referencing a table - Mailing list pgsql-sql

From Andreas Joseph Krogh
Subject Re: Res: Finding all tables that have foreign keys referencing a table
Date
Msg-id 200811072046.10358.andreak@officenet.no
Whole thread Raw
In response to Res: Finding all tables that have foreign keys referencing a table  (paulo matadr <saddoness@yahoo.com.br>)
Responses Re: Res: Finding all tables that have foreign keys referencing a table
List pgsql-sql
On Friday 07 November 2008 15:15:49 paulo matadr wrote:
> Try this
>
> select table_schema, table_name
> from information_schema.columns
> where table_schema not in
> ('information_schema','pg_catalog')
> and column_name = '?'

AFAICS this lists all tables which have a column named '?', which is not what I'm after. I'm after listing all columns
referencinga certain column as a FOREIGN KEY. 

Example, making the FK's names "$1" to mimic old PG-behaviour, and proving my point about the FK-names not needing to
beglobaly unique: 

CREATE TABLE test1(
id INTEGER PRIMARY KEY
);

CREATE TABLE test2(
id INTEGER PRIMARY KEY
);

CREATE TABLE test_ref1(
id INTEGER PRIMARY KEY,
entity_id INTEGER,
CONSTRAINT "$1" FOREIGN KEY (entity_id) REFERENCES test1(id)
);

CREATE TABLE test_ref2(
id INTEGER PRIMARY KEY,
entity_id INTEGER,
CONSTRAINT "$1" FOREIGN KEY (entity_id) REFERENCES test2(id)
);

andreak=# \d test_ref1   Table "public.test_ref1" Column   |  Type   | Modifiers
-----------+---------+-----------id        | integer | not nullentity_id | integer |
Indexes:   "test_ref1_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:   "$1" FOREIGN KEY (entity_id) REFERENCES test1(id)

andreak=# \d test_ref2   Table "public.test_ref2" Column   |  Type   | Modifiers
-----------+---------+-----------id        | integer | not nullentity_id | integer |
Indexes:   "test_ref2_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:   "$1" FOREIGN KEY (entity_id) REFERENCES test2(id)

andreak=#

Now, any idea about how to safely get all columns which reference test1.id?
-hackers; Any hints?

BTW:
andreak=# select version();                                           version
-----------------------------------------------------------------------------------------------PostgreSQL 8.3.3 on
i486-pc-linux-gnu,compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7) 

--
Andreas Joseph Krogh <andreak@officenet.no>
Senior Software Developer / CEO
------------------------+---------------------------------------------+
OfficeNet AS            | The most difficult thing in the world is to |
Karenslyst Allé 11      | know how to do a thing and to watch         |
PO. Box 529 Skøyen      | somebody else doing it wrong, without       |
0214 Oslo               | comment.                                    |
NORWAY                  |                                             |
Tlf:    +47 24 15 38 90 |                                             |
Fax:    +47 24 15 38 91 |                                             |
Mobile: +47 909  56 963 |                                             |
------------------------+---------------------------------------------+


pgsql-sql by date:

Previous
From: "Devil™ Dhuvader"
Date:
Subject: Re: need help in building a query
Next
From: Tom Lane
Date:
Subject: Re: Res: Finding all tables that have foreign keys referencing a table