-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_create.py
More file actions
42 lines (38 loc) · 1015 Bytes
/
pg_create.py
File metadata and controls
42 lines (38 loc) · 1015 Bytes
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
import psycopg2
from connect import create_connection
create_table_tasks_query = """
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
title VARCHAR(100),
description TEXT,
status_id INT,
user_id INT,
FOREIGN KEY (status_id) REFERENCES status (id)
ON DELETE SET NULL
ON UPDATE CASCADE,
FOREIGN KEY (user_id) REFERENCES users (id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
"""
create_table_users_query = """
CREATE TABLE users (
id SERIAL PRIMARY KEY,
fullname VARCHAR(30),
email VARCHAR(30),
CONSTRAINT users_email_un UNIQUE (email)
);
"""
create_table_status_query = """
CREATE TABLE status (
id SERIAL PRIMARY KEY,
name VARCHAR(30),
CONSTRAINT status_name_un UNIQUE (name)
);
"""
if __name__ == '__main__':
with create_connection() as connection:
with connection.cursor() as cursor:
cursor.execute(create_table_status_query)
cursor.execute(create_table_users_query)
cursor.execute(create_table_tasks_query)