-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.sql
45 lines (40 loc) · 1.29 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
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(255) UNIQUE NOT NULL,
password_hash varchar(255) NOT NULL,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
is_admin boolean NOT NULL
);
CREATE TABLE articles(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
title varchar(255) NOT NULL,
announce varchar(2000) NOT NULL,
text varchar(4000) NOT NULL,
image varchar(50),
user_id integer NOT NULL,
created_at timestamp DEFAULT current_timestamp,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE comments(
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
article_id integer NOT NULL,
user_id integer NOT NULL,
text varchar(2000) NOT NULL,
created_at timestamp DEFAULT current_timestamp,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (article_id) REFERENCES articles(id)
);
CREATE TABLE article_category(
article_id integer NOT NULL,
category_id integer NOT NULL,
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);
CREATE INDEX ON users(email);