-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentMajor.py
More file actions
33 lines (29 loc) · 1.6 KB
/
StudentMajor.py
File metadata and controls
33 lines (29 loc) · 1.6 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
from orm_base import Base
from sqlalchemy import UniqueConstraint, ForeignKey, Date
from sqlalchemy.orm import Mapped, mapped_column, relationship
# We canNOT import Student and Major here because Student and Major
# are both importing StudentMajor. So we have to go without the
# ability to validate the Student or Major class references in
# this class. Otherwise, we get a circular import.
# from Student import Student
# from Major import Major
from datetime import datetime
class StudentMajor(Base):
"""The association class between Student and Major. I resorted to using
this style of implementing a Many to Many because I feel that it is the
most versatile approach, and we only have time for one Many to Many
protocol in this class."""
__tablename__ = "student_majors"
major: Mapped["Major"] = relationship(back_populates="students")
majorName: Mapped[str] = mapped_column('major_name', ForeignKey("majors.name"), primary_key=True)
student: Mapped["Student"] = relationship(back_populates="majors")
studentId: Mapped[int] = mapped_column('student_id', ForeignKey("students.student_id"), primary_key=True)
declarationDate: Mapped[Date] = mapped_column('declaration_date', Date, nullable=False)
def __init__(self, student, major, declaration_date: datetime):
self.student = student
self.major = major
self.studentId = student.studentID
self.majorName = major.name
self.declarationDate = declaration_date
def __str__(self):
return f"Student major - student: {self.student} major: {self.major}"