-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
57 lines (38 loc) · 1.5 KB
/
model.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def connect_to_db(app, db_uri="postgresql:///goals"):
"""Connect the database to our Flask app."""
# Configure to use our PostgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# app.config['SQLALCHEMY_ECHO'] = True
db.app = app
db.init_app(app)
class User(db.Model):
"""Users in the system"""
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(100), unique=True, nullable=False)
password = db.Column(db.String(50), nullable=False)
goals = db.relationship('Goal',
backref='users')
def __repr__(self):
return f'<User id={self.id} email={self.email}>'
class Goal(db.Model):
"""Goals added to the database by users"""
__tablename__ = 'goals'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
description = db.Column(db.Text)
def __repr__(self):
return f'<Goal id={self.id}>'
def init_app():
from flask import Flask
app = Flask(__name__)
connect_to_db(app)
print("Connected to DB")
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
init_app()