-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_create_likes_table.sql
More file actions
48 lines (45 loc) · 1.55 KB
/
04_create_likes_table.sql
File metadata and controls
48 lines (45 loc) · 1.55 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
CREATE TABLE users (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
username VARCHAR(30) NOT NULL,
bio VARCHAR(400),
avatar VARCHAR(200),
phone VARCHAR(25),
email VARCHAR(40),
password VARCHAR(50),
status VARCHAR(15),
CHECK(COALESCE(phone, email) IS NOT NULL)
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
url VARCHAR(200) NOT NULL,
caption VARCHAR(240),
lat REAL CHECK(lat IS NULL OR (lat >= -90 AND lat <= 90)),
lng REAL CHECK(lng IS NULL OR (lng >= -180 AND lng <= 180)),
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
contents VARCHAR(240) NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE
);
CREATE TABLE likes (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE,
comment_id INTEGER REFERENCES comments(id) ON DELETE CASCADE,
CHECK(
COALESCE((post_id)::BOOLEAN::INTEGER, 0)
+
COALESCE((comment_id)::BOOLEAN::INTEGER, 0)
= 1
),
UNIQUE(user_id, post_id, comment_id)
);