-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.py
41 lines (30 loc) · 1.54 KB
/
create.py
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
import os
import csv
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
#----------------------------------------------------------------------------------------------
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
#----------------------------------------------------------------------------------------------
def main():
# Creating "authors" table.
db.execute("CREATE TABLE authors (id SERIAL PRIMARY KEY, name VARCHAR NOT NULL)")
db.commit()
print(f"Table 'authors' created.")
# Creating "books" table.
db.execute("CREATE TABLE books (id SERIAL PRIMARY KEY, isbn VARCHAR NOT NULL UNIQUE, title VARCHAR NOT NULL, author_id INTEGER NOT NULL REFERENCES authors, year INTEGER)")
db.commit()
print(f"Table 'books' created.")
# Creating "users" table.
db.execute("CREATE TABLE users (id SERIAL PRIMARY KEY, username VARCHAR NOT NULL UNIQUE, fullname VARCHAR NOT NULL, password VARCHAR NOT NULL)")
db.commit()
print(f"Table 'users' created.")
# Creating "reviews" table.
db.execute("CREATE TABLE reviews (id SERIAL PRIMARY KEY, book_id INTEGER NOT NULL REFERENCES books, user_id INTEGER NOT NULL REFERENCES users, rating INTEGER NOT NULL, review TEXT NOT NULL, pub_date DATE DEFAULT CURRENT_DATE)")
db.commit()
print(f"Table 'reviews' created.")
#----------------------------------------------------------------------------------------------
if __name__ == "__main__":
main()