初始化
This commit is contained in:
3
models/__init__.py
Normal file
3
models/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# 存放数据库表的模型包
|
||||
|
||||
from .lottery_dlt import LotteryDLT
|
||||
108
models/lottery_dlt.py
Normal file
108
models/lottery_dlt.py
Normal file
@@ -0,0 +1,108 @@
|
||||
from datetime import date, datetime
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import Column
|
||||
from sqlmodel import Date, SQLModel, Field, INT, DateTime
|
||||
from pydantic import field_validator
|
||||
|
||||
class LotteryDLT(SQLModel, table=True):
|
||||
"""大乐透彩票数据表"""
|
||||
__tablename__ = "lottery_dlt"
|
||||
|
||||
id: int | None = Field(default=None, primary_key=True, index=True)
|
||||
|
||||
issue: str = Field(
|
||||
unique=True,
|
||||
nullable=False,
|
||||
max_length=10,
|
||||
sa_column_kwargs={"comment": "彩票期号,唯一"}
|
||||
)
|
||||
"""彩票期号"""
|
||||
|
||||
game_name: str = Field(
|
||||
max_length=50,
|
||||
nullable=False,
|
||||
sa_column_kwargs={"comment": "彩票种类名称"}
|
||||
)
|
||||
"""彩票种类名称"""
|
||||
|
||||
draw_date: date = Field(
|
||||
sa_column = Column(Date, nullable=False, comment="开奖日期")
|
||||
)
|
||||
"""开奖日期"""
|
||||
|
||||
draw_time: datetime = Field(
|
||||
sa_column=Column(
|
||||
DateTime(timezone=True),
|
||||
nullable=False,
|
||||
comment="开奖时间"
|
||||
)
|
||||
)
|
||||
"""开奖时间"""
|
||||
|
||||
# 前区排序后的5个号码
|
||||
front_sort_1: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码1"))
|
||||
"""前区排序后的号码1"""
|
||||
front_sort_2: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码2"))
|
||||
"""前区排序后的号码2"""
|
||||
front_sort_3: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码3"))
|
||||
"""前区排序后的号码3"""
|
||||
front_sort_4: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码4"))
|
||||
"""前区排序后的号码4"""
|
||||
front_sort_5: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码5"))
|
||||
"""前区排序后的号码5"""
|
||||
|
||||
# 后区排序后的2个号码
|
||||
back_sort_1: Optional[int] = Field(sa_column=Column(INT, comment="后区排序后的号码1"))
|
||||
"""后区排序后的号码1"""
|
||||
back_sort_2: Optional[int] = Field(sa_column=Column(INT, comment="后区排序后的号码2"))
|
||||
"""后区排序后的号码2"""
|
||||
|
||||
# 前区5个号码
|
||||
front_1: int = Field(sa_column=Column(INT, comment="前区号码1"))
|
||||
"""前区号码1"""
|
||||
front_2: int = Field(sa_column=Column(INT, comment="前区号码2"))
|
||||
"""前区号码2"""
|
||||
front_3: int = Field(sa_column=Column(INT, comment="前区号码3"))
|
||||
"""前区号码3"""
|
||||
front_4: int = Field(sa_column=Column(INT, comment="前区号码4"))
|
||||
"""前区号码4"""
|
||||
front_5: int = Field(sa_column=Column(INT, comment="前区号码5"))
|
||||
"""前区号码5"""
|
||||
|
||||
# 后区2个号码
|
||||
back_1: int = Field(sa_column=Column(INT, comment="后区号码1"))
|
||||
"""后区号码1"""
|
||||
back_2: int = Field(sa_column=Column(INT, comment="后区号码2"))
|
||||
"""后区号码2"""
|
||||
|
||||
# 元数据字段
|
||||
created_at: datetime = Field(
|
||||
default_factory=datetime.now,
|
||||
sa_column_kwargs={"comment": "创建时间"}
|
||||
)
|
||||
"""创建时间"""
|
||||
|
||||
updated_at: datetime = Field(
|
||||
default_factory=datetime.now,
|
||||
sa_column_kwargs={"onupdate": datetime.now, "comment": "更新时间"}
|
||||
)
|
||||
"""更新时间"""
|
||||
|
||||
# Pydantic 校验器:确保号码合法性
|
||||
@field_validator(
|
||||
'front_1', 'front_2', 'front_3', 'front_4', 'front_5',
|
||||
'front_sort_1', 'front_sort_2', 'front_sort_3', 'front_sort_4', 'front_sort_5'
|
||||
)
|
||||
@classmethod
|
||||
def validate_front(cls, v):
|
||||
if not 1 <= v <= 35:
|
||||
raise ValueError(f'前区号码必须在1-35之间,当前值: {v}')
|
||||
return v
|
||||
|
||||
@field_validator('back_1', 'back_2', 'back_sort_1', 'back_sort_2')
|
||||
@classmethod
|
||||
def validate_back(cls, v):
|
||||
if not 1 <= v <= 12:
|
||||
raise ValueError(f'后区号码必须在1-12之间,当前值: {v}')
|
||||
return v
|
||||
Reference in New Issue
Block a user