0

Here is a simple example of my problem

CREATE TABLE parent (
 id SERIAL PRIMARY KEY
);

CREATE TABLE child (
 name text
) INHERITS (parent);

-- Populating child table
INSERT INTO child (name) VALUES ('Alex');
INSERT INTO child (name) VALUES ('Simba');
INSERT INTO child (name) VALUES ('Jafar');


CREATE TABLE test(
 parentId INTEGER REFERENCES parent (id)
)

-- I checked Alex has id of 1 and it is present in parent table
INSERT INTO test(parentId) VALUES (1);-- This line does not work 

I geting the following message: insert into test(parentId) VALUES (1)

ERROR: insert or update on table "test" violates foreign key constraint "test_parentid_fkey" DETAIL: Key (parentid)=(1) is not present in table "parent".

Why do i get this ERROR ?

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
urag
  • 1,228
  • 9
  • 28
  • You have to insert the value inside the `parent` table with `id=1` as, you are using it in your mapping table. Hence, it throws the error. – Arihant Oct 08 '18 at 08:31
  • What do you mean by install as I wrote the record with id =1 is present in parent table because child table inhertce it – urag Oct 08 '18 at 08:33
  • Changed it to insert.. – Arihant Oct 08 '18 at 08:33
  • What does 'SELECT * FROM parent` return. Check if it has any value with `id=1` – Arihant Oct 08 '18 at 08:35
  • 4
    [Quote from the manual](https://www.postgresql.org/docs/current/static/ddl-inherit.html#DDL-INHERIT-CAVEATS): "*A serious limitation of the inheritance feature is that indexes (including unique constraints) and foreign key constraints only apply to single tables, not to their inheritance children*" –  Oct 08 '18 at 08:42

0 Answers0