Re: Inheritance, shared primary keys between parent and child tables.

From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Inheritance, shared primary keys between parent and child tables.
Date: 2014-01-10 17:15:28
Message-ID: 1389374128202-5786295.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

James McCarthy wrote
> I thought the primary key was NOT shared between parent/child tables
> through inheritance.
>
> On reading the documentation section for inheritance in Postgresql
> http://www.postgresql.org/docs/9.1/static/ddl-inherit.html
> It seems obvious that the primary key of the parent table is not shared
> by
> the child table(am I miss-understanding?)
>
> My experience from playing around with this indicates otherwise.
>
> For example
>
> CREATE TABLE account (
> id SERIAL PRIMARY KEY
> );
>
> CREATE TABLE account_admin (
> cash INTEGER
> )INHERITS (account);
>
> CREATE TABLE account_user (
> credit INTEGER
> )INHERITS (account);
>
>
> INSERT INTO account_admin(cash) VALUES(12);
> INSERT INTO account_user(credit) VALUES(12);
> INSERT INTO account_user(credit) VALUES(12);
> INSERT INTO account_admin(cash) VALUES(12);
>
> My understanding of the documentation is:
>
> SELECT id FROM account_admin;
>
> should return 1,2
>
> actually returns: 1,4;
>
> And:
>
> SELECT id FROM account_user;
> should return: 1,2
> actually returns: 2,3
>
> While
> SELECT id FROM account;
> returns: 1,2,3,4 as expected
>
>
> Does this mean that child tables of a parent share the same primary key?

No. It means they share the same sequence for generating DEFAULTs (i.e. it
copies the end-result of a successful SERIAL type construction - not the
SERIAL pseudo-type itself). Try specifying manual IDs and see what happens.

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Inheritance-shared-primary-keys-between-parent-and-child-tables-tp5786288p5786295.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message DFE 2014-01-14 11:32:46 archive_cleanup_command when is called?
Previous Message James McCarthy 2014-01-10 16:51:47 Inheritance, shared primary keys between parent and child tables.