PostgreSQL foreign key not existing, issue of inheritance? -


i struggling foreign keys in db, possibly has inheritance?
here's basic setup:

-- table address create table address (   pk_address serial not null,   fk_gadmid_0 integer not null, -- table exists, no problem here   street character varying(100),   zip character varying(10),   city character varying(50),   public boolean,   constraint address_primarykey primary key (pk_address),   constraint gadmid_0_primarykey foreign key (fk_gadmid_0)       references adm0 (gadmid_0) match simple       on update cascade on delete no action ) (   oids=false ); alter table address owner postgres;    -- table stakeholder (parent) create table stakeholder (     pk_stakeholder integer default nextval('common_stakeholder_seq') not null,     fk_stakeholder_type integer not null, -- table exists, no problem here     name character varying(255) not null,     constraint stakeholder_primarykey primary key (pk_stakeholder),     constraint stakeholder_fk_stakeholder_type foreign key (fk_stakeholder_type)         references stakeholder_type (pk_stakeholder_type) match simple         on update cascade on delete no action ) (     oids=false ); alter table stakeholder owner postgres;    -- table individual (child of stakeholder) create table individual (     firstname character varying(50),     fk_title integer, -- table exists, no problem here     email1 character varying (100),     email2 character varying (100),     phone1 character varying (50),     phone2 character varying (50),     constraint individual_primarykey primary key (pk_stakeholder),     constraint title_foreignkey foreign key (fk_title)         references title (pk_title) match simple         on update cascade on delete cascade ) inherits (stakeholder) (     oids=false ); alter table individual owner postgres;    -- link between stakeholder , address create table l_stakeholder_address (     pk_l_stakeholder_address serial not null,     fk_stakeholder integer not null references stakeholder,     fk_address integer not null references address,     constraint l_stakeholder_address_primarykey primary key (pk_l_stakeholder_address),     constraint l_stakeholder_address_fk_stakeholder foreign key (fk_stakeholder)         references stakeholder (pk_stakeholder) match simple         on update cascade on delete no action,     constraint l_stakeholder_address_fk_address foreign key (fk_address)         references address (pk_address) match simple         on update cascade on delete no action ) (     oids=false ); alter table l_stakeholder_address owner postgres; 

so far, no problem. tried add values:

insert individual (pk_stakeholder, fk_stakeholder_type, name, firstname, fk_title, email1, email2, phone1, phone2)    values (1, 8, 'lastname', 'firstname', 1, 'me@you.com', '', '', ''); insert address (pk_address, fk_gadmid_0, street, zip, city, public)     values (1, 126, 'address', '', 'city', false); insert l_stakeholder_address (pk_l_stakeholder_address, fk_stakeholder, fk_address)     values (default, 1, 1); 

and end having error (sql state 23503) saying key (fk_stakeholder)=(1) not existing in table "stakeholder".
first 2 inserts fine, can see them in databases:

stakeholder: pk_stakeholder | ... ---------------------- 1              | ...  address: pk_address | ... -------------------- 1          | ... 

could please tell me doing wrong? must admit rather new postgresql (using 8.4) i'm not sure if issue of pg @ all, maybe i'm lacking basic database design understandings ...
either way, tried pretty think of, tried make fk deferrable in postgresql : transaction , foreign key problem somehow doesn't work either.
appreciated , many in advance! best regards, lukas

you can work around using additional table individual_pks (individual_pk integer primary key) primary keys both parent , child, maintained using triggers (very simple — insert individual_pks on insert, delete on delete, update on update, if changes individual_pk).

then point foreign keys additional table instead of child. there'll small performance hit, when adding/deleting rows.

or forget inheritance , old way - 1 table nullable columns.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -