-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
95 lines (73 loc) · 2.05 KB
/
models.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from sqlalchemy import Column, String, Date, Integer, create_engine
from flask_sqlalchemy import SQLAlchemy
import os
database_name = "learning"
database_path = "postgres://{}:{}@{}/{}".format(
"postgres", "rohit", "localhost:5432", database_name)
db = SQLAlchemy()
'''
setup_db(app)
'''
def setup_db(app):
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ['DATABASE_URL']
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
'''
Define Students class here
'''
class Students(db.Model):
__tablename__ = 'students'
id = Column(Integer, primary_key=True)
name = Column(String)
age = Column(Integer)
gender = Column(String)
teacher_id = Column(Integer, db.ForeignKey('teachers.id'), nullable=False)
def __init__(self, name, age, gender, teacher_id):
self.name = name
self.age = age
self.gender = gender
self.teacher_id = teacher_id
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'name': self.name,
'age': self.age,
'gender': self.gender,
'teacher_id': self.teacher_id
}
'''
Define Teachers class here
'''
class Teachers(db.Model):
__tablename__ = 'teachers'
id = Column(Integer, primary_key=True)
Name = Column(String)
age = Column(Integer)
students = db.relationship('Students', backref='teachers')
def __init__(self, Name, age):
self.Name = Name
self.age = age
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'Name': self.Name,
'age': self.age,
'students': self.students
}