patternsqlMinor
Violates foreign key constraint
Viewed 0 times
violatesforeignkeyconstraint
Problem
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.
```
-- 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',
```
-- 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',
Solution
Basically foreign key constraints are not inherited. If you are working with table inheritance you have a few options.
-
Stop enforcing foreign keys
-
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.
-
Stop enforcing foreign keys
-
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.
Context
StackExchange Database Administrators Q#28341, answer score: 3
Revisions (0)
No revisions yet.