1
1
from sqlalchemy import Column , String , Integer
2
2
from flask_sqlalchemy import SQLAlchemy
3
- import json
4
-
5
3
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 } '
7
8
8
9
db = SQLAlchemy ()
9
10
12
13
binds a flask application and a SQLAlchemy service
13
14
"""
14
15
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
18
18
db .init_app (app )
19
19
20
20
"""
21
21
Question
22
-
23
22
"""
24
23
class Question (db .Model ):
25
24
__tablename__ = 'questions'
26
25
27
26
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 )
32
31
33
32
def __init__ (self , question , answer , category , difficulty ):
34
33
self .question = question
@@ -54,17 +53,16 @@ def format(self):
54
53
'answer' : self .answer ,
55
54
'category' : self .category ,
56
55
'difficulty' : self .difficulty
57
- }
56
+ }
58
57
59
58
"""
60
59
Category
61
-
62
60
"""
63
61
class Category (db .Model ):
64
62
__tablename__ = 'categories'
65
63
66
64
id = Column (Integer , primary_key = True )
67
- type = Column (String )
65
+ type = Column (String , nullable = False )
68
66
69
67
def __init__ (self , type ):
70
68
self .type = type
@@ -73,4 +71,4 @@ def format(self):
73
71
return {
74
72
'id' : self .id ,
75
73
'type' : self .type
76
- }
74
+ }
0 commit comments