Skip to content

Commit 9e8ba5e

Browse files
committed
Adjust boilerplate code
1 parent d6a6b14 commit 9e8ba5e

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

backend/models.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from sqlalchemy import Column, String, Integer
22
from flask_sqlalchemy import SQLAlchemy
3-
import json
4-
53
database_name = 'trivia'
6-
database_path = 'postgresql://{}/{}'.format('localhost:5432', database_name)
4+
database_user = 'postgres'
5+
database_password = 'password'
6+
database_host = 'localhost:5432'
7+
database_path = f'postgresql://{database_user}:{database_password}@{database_host}/{database_name}'
78

89
db = SQLAlchemy()
910

@@ -12,23 +13,21 @@
1213
binds a flask application and a SQLAlchemy service
1314
"""
1415
def setup_db(app, database_path=database_path):
15-
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
16-
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
17-
db.app = app
16+
app.config['SQLALCHEMY_DATABASE_URI'] = database_path
17+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1818
db.init_app(app)
1919

2020
"""
2121
Question
22-
2322
"""
2423
class Question(db.Model):
2524
__tablename__ = 'questions'
2625

2726
id = Column(Integer, primary_key=True)
28-
question = Column(String)
29-
answer = Column(String)
30-
category = Column(String)
31-
difficulty = Column(Integer)
27+
question = Column(String, nullable=False)
28+
answer = Column(String, nullable=False)
29+
category = Column(String, nullable=False)
30+
difficulty = Column(Integer, nullable=False)
3231

3332
def __init__(self, question, answer, category, difficulty):
3433
self.question = question
@@ -54,17 +53,16 @@ def format(self):
5453
'answer': self.answer,
5554
'category': self.category,
5655
'difficulty': self.difficulty
57-
}
56+
}
5857

5958
"""
6059
Category
61-
6260
"""
6361
class Category(db.Model):
6462
__tablename__ = 'categories'
6563

6664
id = Column(Integer, primary_key=True)
67-
type = Column(String)
65+
type = Column(String, nullable=False)
6866

6967
def __init__(self, type):
7068
self.type = type
@@ -73,4 +71,4 @@ def format(self):
7371
return {
7472
'id': self.id,
7573
'type': self.type
76-
}
74+
}

0 commit comments

Comments
 (0)