-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
60 lines (43 loc) · 1.57 KB
/
Copy pathtest.py
File metadata and controls
60 lines (43 loc) · 1.57 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
50
51
52
53
54
55
56
57
58
59
60
from datetime import date
from sqlalchemy import Integer, String, Date, func, ForeignKey
from sqlalchemy.schema import CreateTable
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
mapped_column,
relationship,
declared_attr,
)
class Base(DeclarativeBase):
pass
class IdMixin:
@declared_attr
def id(cls) -> Mapped[int]:
return mapped_column(Integer, primary_key=True)
class Participation(Base):
__tablename__ = "participations"
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id"), primary_key=True)
employee_id: Mapped[int] = mapped_column(
ForeignKey("employees.id"), primary_key=True
)
role: Mapped[str] = mapped_column(String(50), nullable=False)
class Project(Base, IdMixin):
__tablename__ = "projects"
name: Mapped[str] = mapped_column(String(150), nullable=False)
start_date: Mapped[date] = mapped_column(
Date, server_default=func.now(), nullable=False
)
employees: Mapped[list["Employee"]] = relationship(
back_populates="projects", secondary="participations"
)
class Employee(Base, IdMixin):
__tablename__ = "employees"
name: Mapped[str] = mapped_column(String(100), nullable=False)
email: Mapped[str] = mapped_column(String(120), unique=True, nullable=False)
projects: Mapped[list["Project"]] = relationship(
back_populates="employees", secondary="participations"
)
if __name__ == "__main__":
print(CreateTable(Participation.__table__))
print(CreateTable(Project.__table__))
print(CreateTable(Employee.__table__))