-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathschema.sql
More file actions
59 lines (53 loc) · 1.73 KB
/
schema.sql
File metadata and controls
59 lines (53 loc) · 1.73 KB
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
CREATE TABLE authors (
id integer PRIMARY KEY,
name text NOT NULL CHECK(name <> ''),
date_of_birth date NOT NULL,
date_of_death date,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp
);
-- why on earth anyone would use polymorphic relations is beyond me.
CREATE TABLE photos (
id integer PRIMARY KEY,
title text NOT NULL CHECK(title <> ''),
uri text NOT NULL CHECK(uri <> ''),
imageable_id integer NOT NULL,
imageable_type text NOT NULL CHECK (imageable_type <> ''),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp
);
-- i've arbitrarily chosen this to be the hasOne example of polymorphic rels
CREATE TABLE series (
id integer PRIMARY KEY,
title text NOT NULL CHECK(title <> ''),
photo_id int NOT NULL REFERENCES photos(id),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp
);
CREATE TABLE books (
id integer PRIMARY KEY,
author_id integer NOT NULL REFERENCES authors(id),
series_id integer REFERENCES series(id),
date_published date NOT NULL,
title text NOT NULL CHECK(title <> ''),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp
);
CREATE TABLE chapters (
id integer PRIMARY KEY,
book_id integer NOT NULL REFERENCES books(id),
title text NOT NULL CHECK(title <> ''),
ordering integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp
);
CREATE TABLE stores (
id integer PRIMARY KEY,
name text NOT NULL CHECK(name <> ''),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp
);
CREATE TABLE books_stores (
book_id integer NOT NULL REFERENCES books(id),
store_id integer NOT NULL REFERENCES stores(id)
);