我正在尝试实现下面的 UML,但我无法弄清楚我的错误在哪里,因为我已经在表中放置了一个值。
-- 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';
该错误与代码的最后一部分有关。
错误:在表“container”上插入或更新违反了外键约束“container_gobject_fkey”详细信息:表“graphobject”中不存在键(gobject)=(6)。
********** 错误 **********
错误:在表“容器”上插入或更新违反外键约束“container_gobject_fkey”SQL 状态:23503 详细信息:表“graphobject”中不存在键 (gobject)=(6)。
更新
好的,我误解了继承。任何人都可以建议我如何实现和, , ,contains
之间的关系。GraphPanel
GraphObject
Tree
Arrow
House
基本上外键约束不会被继承。如果您正在使用表继承,您有几个选择。
停止强制执行外键
使用约束触发器强制执行外键
在大多数情况下,最好使用单个大表和可能带有延迟外键的较小连接表。不幸的是,这些也不可能完全完成表继承可以完成的事情,因此通常至少会有一些自定义编码。