I have a PostgreSQL database, 3 tables and my schema as follows
--- Parent Table
CREATE TABLE IF NOT EXISTS abc.parent(
record_id SERIAL PRIMARY KEY,
description text NOT NULL
);
--- Child Table
CREATE TABLE IF NOT EXISTS abc.child (
total_count INT NOT NULL) INHERITS (abc.parent);
-- Detail
CREATE TABLE abc.detail(
detail_id int NOT NULL,
detail_description text NOT NULL
record_id int NOT NULL,
FOREIGN KEY (record_id) REFERENCES abc.parent(record_id)
);
Then i insert records in to both Parent and Child tables .
Parent
|record_id|description|
|1 |abcd |
|2 |efgh |
child
|record_id|description|total_count|
|3 |xygh |5 |
|4 |mnop |7 |
When I try to insert record into detail table following two entires was success
Detail
|detail_id|detail_description|record_id|
|100 |detail_desc1 | 1 |
|200 |detail_desc2 | 2 |
but I cant insert entry with record_id 3 it gave me a foreign key violation error
Can someone explain this error ??
Can we create foreign key relationship like this in Postgresql with inheritance