4

I'm trying to implement the below UML and I can't figure out where my mistake is since I have already place a value into the table.

UML class diagram

-- Schema: public

DROP SCHEMA IF EXISTS public CASCADE;

CREATE SCHEMA public
  AUTHORIZATION postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
COMMENT ON SCHEMA public IS 'standard public schema';

CREATE TYPE color AS ENUM ('green','black','yellow','grey','blue');
CREATE TYPE arrowtype AS ENUM ('both_arrows','begin_arrow','end_arrow'); 
CREATE TYPE treetype AS ENUM ('orange_tree','apple_tree');
CREATE TYPE linetype AS ENUM ('hairline','solid','dotted','dashed');

CREATE TYPE address AS (
street_name TEXT,
street_no TEXT
);

CREATE TYPE square AS (
row VARCHAR(10),
colmn VARCHAR(10)
);

CREATE TABLE user_table (
username VARCHAR(30) NOT NULL PRIMARY KEY,
password VARCHAR(16),
name     VARCHAR(100),
lastname VARCHAR(100),
telephone VARCHAR(20)[],
address address,
birth_year INTEGER
);

CREATE TABLE GraphPanel (
id SERIAL NOT NULL PRIMARY KEY,
title VARCHAR(255),
creation_date TIMESTAMP,
comments TEXT
);

CREATE TABLE GraphObject (
id SERIAL NOT NULL PRIMARY KEY,
color color,
title VARCHAR(255)
);

CREATE TABLE House (
roof_color color,
roof_height INTEGER,
position square
)INHERITS(GraphObject);

CREATE TABLE Arrow (
arrow_line linetype,
arrow_type arrowtype
)INHERITS(GraphObject);

CREATE TABLE Tree (
position square,
tree_type treetype
)INHERITS(GraphObject);

CREATE TABLE creator (
username VARCHAR(30),
panel INTEGER,
category VARCHAR(50),
FOREIGN KEY (username) REFERENCES user_table(username),
FOREIGN KEY (panel) REFERENCES GraphPanel(id)
);

CREATE TABLE container (
panel  INTEGER,
gobject INTEGER,
FOREIGN KEY (panel) REFERENCES GraphPanel(id),
FOREIGN KEY (gobject) REFERENCES GraphObject(id)
);

-- INSERTING VALUES 

-- Users

INSERT INTO user_table (
    username,
    password,name,
    lastname,
    telephone,
    address,
    birth_year
) VALUES (
    'giannis',
    'thepassword',
    'Giannis',
    'Christofakis',
    '{"6944789333","2831051300"}',
    ROW('Evridikis','7'),
    1985
);

INSERT INTO user_table (
    username,
    password,
    name,
    lastname,
    telephone,
    address,
    birth_year
) VALUES (
    'antonis',
    'psw',
    'Antonis',
    'Papadakis',
    '{"6974246700","2831051312"}',
    ROW('Doukos Mpofor','12'),
    1993
);

INSERT INTO user_table (
    username,
    password,
    name,
    lastname,
    telephone,
    address,
    birth_year
) VALUES (
    'mpampis',
    'thepsw',
    'Mpampis',
    'Theodorou',
    '{"6984652333","284131100"}',
    ROW('Markoy Portaliou','8'),
    1981
);

-- SELECT * FROM user_table;

-- GraphPanels

INSERT INTO GraphPanel (
    title,
    creation_date,
    comments
) VALUES (
    'Platia Sinani',
    '2004-10-19 10:23:54',
    'Diamorfosi ths platias Sinani'
);

INSERT INTO GraphPanel (
    title,
    creation_date,
    comments
) VALUES (
    'Parking TEI',
    '2008-11-04 22:20:01',
    'Apeikonisi parking tou TEI'
);

INSERT INTO GraphPanel (
    title,
    creation_date,
    comments
) VALUES (
    'Perioxh Stavormenou',
    '2011-05-17 18:23:33',
    'Dimhourgeia parkou sth perioxh tou Estavromenou'
);

INSERT INTO GraphPanel (
    title,
    creation_date,
    comments
) VALUES (
    'Odos Panagiotaki',
    '2000-09-22 11:00:33',
    'Kataskeuh gefuras'
);

-- SELECT * FROM GraphPanel;

-- creator

INSERT INTO creator (
    username,
    panel,
    category
) VALUES (
    'giannis',
     1,
    'Diamorfosh'
);

INSERT INTO creator (
    username,
    panel,
    category
) VALUES (
    'mpampis',
     2,
    'Apeikonish'
);

INSERT INTO creator (
    username,
    panel,
    category
) VALUES (
    'antonis',
     3,
    'Kataskeuh'
);

INSERT INTO creator (
    username,
    panel,
    category
) VALUES (
    'giannis',
     4,
    'Kataskeuh'
);

-- SELECT * FROM creator;

-- GraphObjects

-- House

INSERT INTO HOUSE (
    color,
    title,
    roof_color,
    roof_height,
    position
) VALUES (
    'grey',
    'Ate Bank',
    'green',
     6,
     ROW('12','A3')
);

INSERT INTO HOUSE (
    color,
    title,
    roof_color,
    roof_height,
    position
) VALUES (
    'grey',
    'residents house',
    'blue',
     4,
     ROW('23','Y3')
);

INSERT INTO HOUSE (
    color,
    title,
    roof_color,
    roof_height,
    position
) VALUES (
    'blue',
    'Super Market',
    'yellow',
     7,
     ROW('08','L2')
);

-- SELECT * FROM House;

-- Trees

INSERT INTO Tree (
    color,
    title,
    position,
    tree_type
) VALUES (
    'green',
    'valencia',
     ROW('07','L2'),
     'orange_tree'
);

INSERT INTO Tree (
    color,
    title,
    position,
    tree_type
) VALUES (
    'green',
    'mantarinia',
     ROW('18','J2'),
     'orange_tree'
);

INSERT INTO Tree (
    color,
    title,
    position,
    tree_type
) VALUES (
    'green',
    'firikia',
     ROW('03','E1'),
     'apple_tree'
);

-- SELECT * FROM Tree;

INSERT INTO Arrow (
    color,
    title,
    arrow_line,
    arrow_type
) VALUES (
    'black',
    'starting point',
     'solid',
     'begin_arrow'
);

INSERT INTO Arrow (
    color,
    title,
    arrow_line,
    arrow_type
) VALUES (
    'grey',
    'ending way',
     'dashed',
     'end_arrow'
);

INSERT INTO Arrow (
    color,
    title,
    arrow_line,
    arrow_type
) VALUES (
    'black',
    'both ways',
     'dotted',
     'both_arrows'
);

 SELECT * FROM GraphObject order by id;

-- container



INSERT INTO container (
    panel,
    gobject
) VALUES (
     1,
     6
);


SELECT * FROM container;

-- SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

The error is related to the last portion of the code.

ERROR: insert or update on table "container" violates foreign key constraint "container_gobject_fkey" DETAIL: Key (gobject)=(6) is not present in table "graphobject".

********** Error **********

ERROR: insert or update on table "container" violates foreign key constraint "container_gobject_fkey" SQL state: 23503 Detail: Key (gobject)=(6) is not present in table "graphobject".


UPDATE

OK I misunderstood inheritance. Can anyone advice me how to implement the contains relationship between GraphPanel and GraphObject,Tree,Arrow,House.

14
  • 3
    Do you ever insert an id of 6 into GraphObject? The error message seems pretty clear.
    – JNK
    Nov 7, 2012 at 20:33
  • I'm inserting a Tree which inherits from GraphObject so there it is. Isn't my assumption right? Nov 7, 2012 at 20:42
  • 2
    Well you are inserting into Tree but not GraphObject. If I pay you a dollar does it show up in your parent's bank account? Does the dollar multiply itself so that your parents and their parents and everyone who preceded you also gets that same dollar?
    – JNK
    Nov 7, 2012 at 20:44
  • 1
    If I understand you correctly, you simply need foreign keys on House, Tree etc. referencing GraphObject. Have you checked your tables after cretaing them? I think you will find more columns there than you'd expect. But, most importantly, if you need help on database design, you'd better describe your needs in plain English sentences. It is always clear, while a UML diagram may have its own problems.
    – dezso
    Nov 7, 2012 at 23:36
  • 1
    @giannischristofakis this is a 6-years old question, with an accepted answer. I suggest you add a new question. Apr 11, 2018 at 15:14

1 Answer 1

3

Basically foreign key constraints are not inherited. If you are working with table inheritance you have a few options.

  1. Stop enforcing foreign keys

  2. Use constraint triggers to enforce foreign keys

In most cases you are better off with a single large table and smaller join tables possibly with deferred foreign keys. Unfortunately these are not possible either to fully do the things that table inheritance can do so typically there is going to be at least some custom coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.