Re: Res: Finding all tables that have foreign keys referencing a table

From: Andreas Joseph Krogh <andreak(at)officenet(dot)no>
To: pgsql-sql(at)postgresql(dot)org
Cc: paulo matadr <saddoness(at)yahoo(dot)com(dot)br>
Subject: Re: Res: Finding all tables that have foreign keys referencing a table
Date: 2008-11-07 19:46:10
Message-ID: 200811072046.10358.andreak@officenet.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 referencing a 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 be globaly 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 null
entity_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 null
entity_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(at)officenet(dot)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 | |
------------------------+---------------------------------------------+

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2008-11-07 20:09:33 Re: Res: Finding all tables that have foreign keys referencing a table
Previous Message Devil™ Dhuvader 2008-11-07 17:20:06 Re: need help in building a query