-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.sql
71 lines (62 loc) · 2.26 KB
/
init_db.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
BEGIN;
DROP TABLE IF EXISTS "recipes_to_glasses" CASCADE;
DROP TABLE IF EXISTS "ingredients_to_recipes" CASCADE;
DROP TABLE IF EXISTS "recipes" CASCADE;
DROP TABLE IF EXISTS "glasses" CASCADE;
DROP TABLE IF EXISTS "purchases" CASCADE;
DROP TABLE IF EXISTS "ingredients" CASCADE;
DROP TABLE IF EXISTS "ingredient_classes" CASCADE;
CREATE TABLE "ingredient_classes" (
id SERIAL NOT NULL,
name VARCHAR(1024) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE "ingredients" (
id SERIAL NOT NULL,
class INTEGER,
name VARCHAR(1024) NOT NULL,
available BOOLEAN NOT NULL,
notForRecipes BOOLEAN NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (class) REFERENCES "ingredient_classes" (id) ON DELETE SET NULL
);
CREATE TABLE "purchases" (
ingredient INTEGER NOT NULL,
date DATE NOT NULL,
location VARCHAR(1024) NOT NULL,
price INTEGER NOT NULL,
volume INTEGER NOT NULL,
unit VARCHAR(1024) NOT NULL,
FOREIGN KEY (ingredient) REFERENCES "ingredients" (id) ON DELETE CASCADE
);
CREATE TABLE "recipes" (
id SERIAL NOT NULL,
name VARCHAR(1024),
garnish VARCHAR(1024) NOT NULL,
instructions VARCHAR(10240) NOT NULL,
for_ingredient_id INTEGER UNIQUE,
PRIMARY KEY (id),
FOREIGN KEY (for_ingredient_id) REFERENCES "ingredients" (id) ON DELETE CASCADE
);
CREATE TABLE "glasses" (
id SERIAL NOT NULL,
name VARCHAR(1024) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE "recipes_to_glasses" (
glass_id INTEGER NOT NULL,
recipe_id INTEGER NOT NULL,
FOREIGN KEY (recipe_id) REFERENCES "recipes" (id) ON DELETE CASCADE,
FOREIGN KEY (glass_id) REFERENCES "glasses" (id) ON DELETE CASCADE
);
CREATE TABLE "ingredients_to_recipes" (
recipe_id INTEGER NOT NULL,
ingredient_id INTEGER,
ingredient_class_id INTEGER,
amount_numer INTEGER NOT NULL,
amount_denom INTEGER NOT NULL,
unit VARCHAR(1024) NOT NULL,
FOREIGN KEY (recipe_id) REFERENCES "recipes" (id) ON DELETE CASCADE,
FOREIGN KEY (ingredient_id) REFERENCES "ingredients" (id) ON DELETE CASCADE
);
COMMIT;