mirror of
https://github.com/Cccc-owo/CheckInApp.git
synced 2026-06-17 05:56:29 +00:00
refactor(backend): adopt typed SQLAlchemy models
This commit is contained in:
@@ -1,35 +1,38 @@
|
||||
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Index
|
||||
from sqlalchemy.orm import relationship
|
||||
from datetime import datetime, timezone
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import DateTime, ForeignKey, Index, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from backend.models.database import Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from backend.models.check_in_task import CheckInTask
|
||||
|
||||
|
||||
class CheckInRecord(Base):
|
||||
"""打卡记录模型"""
|
||||
|
||||
__tablename__ = "check_in_records"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
|
||||
task_id = Column(
|
||||
Integer,
|
||||
id: Mapped[int] = mapped_column(primary_key=True, index=True, autoincrement=True)
|
||||
task_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("check_in_tasks.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
index=True,
|
||||
comment="任务 ID",
|
||||
)
|
||||
status = Column(
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(20),
|
||||
nullable=False,
|
||||
index=True,
|
||||
comment="状态: success/failure/out_of_time/unknown/pending",
|
||||
)
|
||||
response_text = Column(Text, default="", comment="响应文本")
|
||||
error_message = Column(Text, default="", comment="错误信息")
|
||||
location = Column(Text, default="{}", comment="位置信息 JSON")
|
||||
trigger_type = Column(
|
||||
response_text: Mapped[str] = mapped_column(Text, default="", comment="响应文本")
|
||||
error_message: Mapped[str] = mapped_column(Text, default="", comment="错误信息")
|
||||
location: Mapped[str] = mapped_column(Text, default="{}", comment="位置信息 JSON")
|
||||
trigger_type: Mapped[str] = mapped_column(
|
||||
String(50), default="scheduled", comment="触发类型: scheduled/manual/admin"
|
||||
)
|
||||
check_in_time = Column(
|
||||
check_in_time: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
index=True,
|
||||
@@ -37,7 +40,7 @@ class CheckInRecord(Base):
|
||||
)
|
||||
|
||||
# 关联任务
|
||||
task = relationship("CheckInTask", back_populates="check_in_records")
|
||||
task: Mapped["CheckInTask"] = relationship(back_populates="check_in_records")
|
||||
|
||||
# 添加复合索引:加速常见查询
|
||||
__table_args__ = (
|
||||
|
||||
Reference in New Issue
Block a user