forked from stevencn76/python_sqlalchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_init.py
More file actions
64 lines (39 loc) · 1.7 KB
/
db_init.py
File metadata and controls
64 lines (39 loc) · 1.7 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
61
62
63
64
import datetime
from sqlalchemy import create_engine, String, ForeignKey
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from typing_extensions import Annotated
from typing import List
class Base(DeclarativeBase):
pass
class Base2(DeclarativeBase):
pass
engine = create_engine('mysql://root:test@localhost/testdb', echo=True)
engine2 = create_engine('mysql://root:test@localhost/myblog_db', echo=True)
int_pk = Annotated[int, mapped_column(primary_key=True)]
required_unique_string = Annotated[str, mapped_column(String(128), unique=True, nullable=False)]
required_string = Annotated[str, mapped_column(String(128), nullable=False)]
timestamp_not_null = Annotated[datetime.datetime, mapped_column(nullable=False)]
class User(Base2):
__tablename__ = "users"
id: Mapped[int_pk]
name: Mapped[required_unique_string]
def __repr__(self):
return f'id: {self.id}, name: {self.name}'
class Department(Base):
__tablename__ = "department"
id: Mapped[int_pk]
name: Mapped[required_unique_string]
employees: Mapped[List["Employee"]] = relationship(back_populates="department")
def __repr__(self):
return f'id: {self.id}, name: {self.name}'
class Employee(Base):
__tablename__ = "employee"
id: Mapped[int_pk]
dep_id: Mapped[int] = mapped_column(ForeignKey("department.id"))
name: Mapped[required_unique_string]
birthday: Mapped[timestamp_not_null]
department: Mapped[Department] = relationship(back_populates="employees")
def __repr__(self):
return f'id: {self.id}, dep_id: {self.dep_id}, name: {self.name}, birthday: {self.birthday}'
Base.metadata.create_all(engine)
Base2.metadata.create_all(engine2)