Estou tentando implementar a UML abaixo e não consigo descobrir onde está meu erro, pois já coloquei um valor na tabela.
-- 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';
O erro está relacionado à última parte do código.
ERRO: inserção ou atualização na tabela "container" viola a restrição de chave estrangeira "container_gobject_fkey" DETALHE: Chave (gobject)=(6) não está presente na tabela "graphobject".
********** Erro **********
ERRO: inserção ou atualização na tabela "container" viola a restrição de chave estrangeira "container_gobject_fkey" Estado SQL: 23503 Detalhe: Chave (gobject)=(6) não está presente na tabela "graphobject".
ATUALIZAR
OK, eu não entendi herança. Alguém pode me aconselhar como implementar a contains
relação entre GraphPanel
e GraphObject
, Tree
, Arrow
, House
.
Basicamente, as restrições de chave estrangeira não são herdadas. Se você estiver trabalhando com herança de tabelas, terá algumas opções.
Parar de impor chaves estrangeiras
Use gatilhos de restrição para impor chaves estrangeiras
Na maioria dos casos, você fica melhor com uma única tabela grande e tabelas de junção menores, possivelmente com chaves estrangeiras adiadas. Infelizmente, também não é possível fazer totalmente as coisas que a herança de tabela pode fazer; normalmente, haverá pelo menos alguma codificação personalizada.