-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
51 lines (34 loc) · 1.77 KB
/
Copy pathmodels.py
File metadata and controls
51 lines (34 loc) · 1.77 KB
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
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from database import Base
class Enrollment(Base):
__tablename__ = "enrollments"
student_id = Column(Integer, ForeignKey("students.id"), primary_key=True)
course_id = Column(Integer, ForeignKey("courses.id"), primary_key=True)
grade = Column(String, nullable=True)
student = relationship("Student", back_populates="enrollments")
course = relationship("Course", back_populates="enrollments")
class Department(Base):
__tablename__ = "departments"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, unique=True, nullable=False, index=True)
courses = relationship("Course", back_populates="department")
class Teacher(Base):
__tablename__ = "teachers"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False, index=True)
courses = relationship("Course", back_populates="teacher")
class Course(Base):
__tablename__ = "courses"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, nullable=False, index=True)
department_id = Column(Integer, ForeignKey("departments.id"), nullable=False)
teacher_id = Column(Integer, ForeignKey("teachers.id"), nullable=True)
department = relationship("Department", back_populates="courses")
teacher = relationship("Teacher", back_populates="courses")
enrollments = relationship("Enrollment", back_populates="course", cascade="all, delete-orphan")
class Student(Base):
__tablename__ = "students"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False, index=True)
enrollments = relationship("Enrollment", back_populates="student", cascade="all, delete-orphan")