forked from htmlacademy-nodejs/144889-typoteka-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
54 lines (48 loc) · 1.42 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
DROP TABLE IF EXISTS categories;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS articles;
DROP TABLE IF EXISTS comments;
DROP TABLE IF EXISTS articles_categories;
CREATE TABLE categories
(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name varchar(255) NOT NULL
);
CREATE TABLE users
(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
email varchar(100) UNIQUE NOT NULL,
password_hash varchar(255) NOT NULL,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
avatar varchar(50) NOT NULL
);
CREATE TABLE articles
(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
title varchar(255) NOT NULL,
announce text NOT NULL,
fullText text NOT NULL,
created_at timestamp DEFAULT current_timestamp,
user_id integer NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id)
);
CREATE TABLE comments
(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
text text NOT NULL,
created_at timestamp DEFAULT current_timestamp,
user_id integer NOT NULL,
article_id integer NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id),
FOREIGN KEY (article_id) REFERENCES articles (id)
);
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),
FOREIGN KEY (category_id) REFERENCES categories (id)
);
CREATE INDEX ON articles (title);