-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelClasses.py
76 lines (59 loc) · 2.29 KB
/
ModelClasses.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
#!/usr/bin/python
from DatabaseConnection import DatabaseConnection
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import mapper, relation, exc, column_property, validates
from sqlalchemy import orm
from sqlalchemy.orm.session import Session
dbc = DatabaseConnection()
# ========================
# Define database classes
# ========================
Base = declarative_base(bind=dbc.engine)
class Student(Base):
__tablename__ = 'student'
__table_args__ = {'autoload' : True}
class Status(Base):
__tablename__ = 'status'
__table_args__ = {'autoload' : True}
class Club(Base):
__tablename__ = 'club'
__table_args__ = {'autoload' : True}
class StudentToClub(Base):
__tablename__ = 'student_to_club'
__table_args__ = {'autoload' : True}
class City(Base):
__tablename__ = 'city'
__table_args__ = {'autoload' : True}
class Supervisor(Base):
__tablename__ = 'supervisor'
__table_args__ = {'autoload' : True}
class StudentToSupervisor(Base):
__tablename__ = 'student_to_supervisor'
__table_args__ = {'autoload' : True}
# =========================
# Define relationships here
# =========================
Student.clubs = relation(Club,
secondary=StudentToClub.__table__,
backref="students")
Student.status = relation(Status, backref="students")
Student.supervisors = relation(Supervisor,
secondary=StudentToSupervisor.__table__,
backref="students")
# Student.clubs = relation(Club,
# secondary=StudentToClub.__table__, # the join table
# primaryjoin=Student.id==StudentToClub.student_id,
# secondaryjoin=StudentToClub.club_id==Club.id, # note that this is the Table, not the object class!
# foreign_keys=[StudentToClub.student_id,StudentToClub.club_id],
# backref="students")
# Student.status = relation(Status,
# primaryjoin=Student.status_id==Status.id,
# foreign_keys=[Student.status_id],
# backref="students")
# Student.supervisors = relation(Supervisor,
# secondary=StudentToSupervisor.__table__,
# primaryjoin=Student.id==StudentToSupervisor.student_id,
# secondaryjoin=StudentToSupervisor.supervisor_id==Supervisor.id,
# foreign_keys=[StudentToSupervisor.student_id, StudentToSupervisor.supervisor_id],
# backref="students")