INHERIT

Started by Peter Harveyover 24 years ago3 messageshackers
Jump to latest
#1Peter Harvey
pharvey@codebydesign.com

If I try to get the columns from pg_attribute using the oid of a child
table created with INHERIT I get its columns AND all of its inherited
columns.

How do I just get the columns added by the child table?

I figure I could check each column to see if they also exist in
pg_attribute under a parent table but I figure there must be an
easier/faster way? Another pg_* table perhaps? Besides; I am not certian
this would work if there is a common column between a child and one of
its parents (if that is even allowed)?

As usual, any help would be greatly appreciated.

Peter

BTW: I checked pg_dump source and did not see an answer.

-- 
+---------------------------
| Data Architect
| your data; how you want it
| http://www.codebydesign.com
+---------------------------
#2Hannu Krosing
hannu@tm.ee
In reply to: Peter Harvey (#1)
Re: INHERIT

Oliver Elphick wrote:

Peter Harvey wrote:

If I try to get the columns from pg_attribute using the oid of a child
table created with INHERIT I get its columns AND all of its inherited
columns.

How do I just get the columns added by the child table?

I figure I could check each column to see if they also exist in
pg_attribute under a parent table but I figure there must be an
easier/faster way? Another pg_* table perhaps? Besides; I am not certian
this would work if there is a common column between a child and one of
its parents (if that is even allowed)?

hannu=# \d parent 
         Table "parent"
 Attribute |  Type   | Modifier 
-----------+---------+----------
 parid     | integer | not null
Index: parent_pkey

hannu=# create table badchild (parid text) inherits (parent);
NOTICE: CREATE TABLE: merging attribute "parid" with inherited
definition
ERROR: CREATE TABLE: attribute "parid" type conflict (int4 and text)
hannu=#

And anyway, in the current state I would advise you not to put too much
hope in postgreSQL's OO features, especially inheritance ;)

-------------
Hannu

#3Oliver Elphick
olly@lfix.co.uk
In reply to: Hannu Krosing (#2)
Re: INHERIT

Peter Harvey wrote:

If I try to get the columns from pg_attribute using the oid of a child
table created with INHERIT I get its columns AND all of its inherited
columns.

How do I just get the columns added by the child table?

I figure I could check each column to see if they also exist in
pg_attribute under a parent table but I figure there must be an
easier/faster way? Another pg_* table perhaps? Besides; I am not certian
this would work if there is a common column between a child and one of
its parents (if that is even allowed)?

As usual, any help would be greatly appreciated.

SELECT a.attname
FROM pg_attribute AS a,
pg_class AS c,
pg_inherits AS i
WHERE a.attrelid = c.oid AND
i.inhrelid = c.oid AND
c.relname = 'child_table'
AND a.attname NOT IN (
SELECT a.attname
FROM pg_attribute AS a,
pg_class AS c,
pg_inherits AS i
WHERE i.inhrelid = c.oid AND
a.attrelid = i.inhparent AND
c.relname = 'child_table'
)

--
Oliver Elphick Oliver.Elphick@lfix.co.uk
Isle of Wight http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47 6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"Submit yourselves therefore to God. Resist the devil,
and he will flee from you." James 4:7