Received: from bigglesworth.mail.be.easynet.net (bigglesworth.mail.be.easynet.net [212.100.160.67]) by postgresql.org (Postfix) with ESMTP id 564424759B8 for ; Thu, 18 Jul 2002 04:41:31 -0400 (EDT) Received: from bidibule.office.be.easynet.net ([212.100.163.150]) by bigglesworth.mail.be.easynet.net with esmtp (Exim 3.35 #1) id 17V6qq-00066X-00 for pgsql-admin@postgresql.org; Thu, 18 Jul 2002 10:41:32 +0200 Received: from rb by bidibule.office.be.easynet.net with local (Exim 3.35 #1 (Debian)) id 17V6np-0000HQ-00 for ; Thu, 18 Jul 2002 10:38:25 +0200 Date: Thu, 18 Jul 2002 10:38:25 +0200 From: Raphael Bauduin To: pgsql-admin@postgresql.org Subject: Inheritance and primary keys Message-ID: <20020718083825.GA607@raphael> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.28i X-Archive-Number: 200207/258 X-Sequence-Number: 4943 Hi, I'm developing a little project management tool in which you have contacts related to a customer, and contacts related to the project. Both contacts contains the same data for now, but for my test, I made the asumption that I would link the contacts directly with the tables customer and project (so customer_contactshas a field cust_idwhich is a foreig key). This is somwhat similar to the example given inthe tutorial with cities, some of which being capitals. I thus create a table contacts, from which the tables project_contacts and cutomer_contacts wil inherit: CREATE TABLE "contacts" ( "contact_id" serial NOT NULL, "contact_first_name" text, "contact_name" character varying(100), "contact_phone" character varying(100), "contact_email" character varying(100), "contact_mobile" character varying(100), Constraint "contacts_pkey" Primary Key ("contact_id") ); When creating the children table, the primary key is not inherited, so I specified a constaint about it (and I removed the foreign key constraint from the text below): create table project_contacts ( project_id bigint,Constraint "project_contacts_pkey" Primary Key ("contact_id")) INHERITS (contacts); create table customer_contacts ( cust_id bigint,Constraint "customer_contacts_pkey" Primary Key ("contact_id")) INHERITS (contacts); Now the strange thing (for me :-) When inserting rows in the different tables, I can get several rows with the same contact_id, though they use the same sequence to set the value of the field contact_id as shown below: \d contacts Table "contacts" Column | Type | Modifiers ------------------+------------------------+------------------------------------------------------------- contact_id | integer |not null default nextval('"contacts_contact_id_seq"'::text) \d customer_contacts; Table "customer_contacts" Column | Type | Modifiers --------------------+------------------------+----------------------------------------------------------- contact_id | integer |not null default nextval('"contacts_contact_id_seq"'::text) \d project_contacts Table "project_contacts" Column | Type | Modifiers ------------------+------------------------+------------------------------------------------------------- contact_id | integer |not null default nextval('"contacts_contact_id_seq"'::text) But as shown below, you can have multiple rows with the same contact_id: select contact_id from contacts; contact_id ------------ 2 3 4 5 6 7 8 9 10 11 5 6 8 9 10 11 12 13 14 15 16 17 18 19 20 1 (26 rows) Am I missing something? Should I read a little more about OO databases? Thanks in advance for your help. Raph PS: to those who read the mail to this point, thanks already :-))