-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.sql
69 lines (59 loc) · 1.89 KB
/
schema.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
DROP TABLE IF EXISTS articles_categories;
DROP TABLE IF EXISTS categories;
DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS articles;
DROP TABLE IF EXISTS users;
CREATE TABLE users
(
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
is_author BOOLEAN NOT NULL,
avatar VARCHAR(255)
);
CREATE TABLE categories
(
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
label VARCHAR(30) NOT NULL UNIQUE
);
CREATE TABLE articles
(
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
user_id INTEGER NOT NULL,
title VARCHAR(250) NOT NULL UNIQUE,
announce VARCHAR(250) NOT NULL,
photo VARCHAR(250),
published_at timestamptz NOT NULL DEFAULT now(),
full_text VARCHAR(1000),
FOREIGN KEY (user_id) REFERENCES users (id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
CREATE TABLE comments
(
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
text VARCHAR(1000),
published_at TIMESTAMPTZ NOT NULL DEFAULT now(),
article_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
FOREIGN KEY (article_id) REFERENCES articles (id)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users (id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE articles_categories
(
article_id INTEGER NOT NULL,
category_id INTEGER NOT NULL,
CONSTRAINT articles_categories_pk PRIMARY KEY (article_id, category_id),
FOREIGN KEY (article_id) REFERENCES articles (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (category_id) REFERENCES categories (id)
ON DELETE CASCADE
ON UPDATE CASCADE
);