This repository was archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
57 lines (42 loc) · 1.64 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
from sqlalchemy import CHAR, ForeignKey, SmallInteger, String
from sqlalchemy.orm import declarative_base, Mapped, mapped_column, \
MappedColumn
Base = declarative_base()
class Column:
@staticmethod
def primary() -> MappedColumn:
return mapped_column(primary_key=True)
@staticmethod
def foreign(name: str) -> MappedColumn:
return mapped_column(ForeignKey(name + 's.id',
ondelete='CASCADE',
onupdate='CASCADE'))
@staticmethod
def data(size: int = 100, fixed: bool = False) -> MappedColumn:
return mapped_column(CHAR(size) if fixed else String(size),
nullable=False,
unique=True)
class Group(Base):
__tablename__ = 'groups'
id: Mapped[int] = Column.primary()
name: Mapped[str] = Column.data(5, True)
class Student(Base):
__tablename__ = 'students'
id: Mapped[int] = Column.primary()
name: Mapped[str] = Column.data()
group_id: Mapped[int] = Column.foreign('group')
class Teacher(Base):
__tablename__ = 'teachers'
id: Mapped[int] = Column.primary()
name: Mapped[str] = Column.data()
class Subject(Base):
__tablename__ = 'subjects'
id: Mapped[int] = Column.primary()
name: Mapped[str] = Column.data()
teacher_id: Mapped[int] = Column.foreign('teacher')
class Grade(Base):
__tablename__ = 'grades'
id: Mapped[int] = Column.primary()
value: Mapped[int] = mapped_column(SmallInteger, nullable=False)
student_id: Mapped[int] = Column.foreign('student')
subject_id: Mapped[int] = Column.foreign('subject')