初始提交:项目基础结构
- backend: FastAPI 后端服务 (Python) - frontend: React + Tauri 前端应用 - docker-compose.yml: 容器编排配置
This commit is contained in:
50
backend/app/models/product.py
Normal file
50
backend/app/models/product.py
Normal file
@ -0,0 +1,50 @@
|
||||
"""产品模型"""
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import String, DateTime, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class Product(Base):
|
||||
__tablename__ = "products"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4,
|
||||
)
|
||||
serial_number: Mapped[str] = mapped_column(
|
||||
String(16), unique=True, index=True, nullable=False, comment="产品序列号(16位)",
|
||||
)
|
||||
|
||||
# ---- 物理外键(关联本库 production_orders) ----
|
||||
order_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("production_orders.id"), nullable=False, comment="所属订单ID",
|
||||
)
|
||||
|
||||
# ---- 逻辑外键(关联老系统物料表,仅存储 ID,无物理约束) ----
|
||||
material_id: Mapped[str | None] = mapped_column(
|
||||
String(64), nullable=True, comment="物料ID(逻辑外键→老系统)",
|
||||
)
|
||||
|
||||
# ---- 物理外键(自引用:父产品) ----
|
||||
parent_product_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("products.id"), nullable=True, comment="父产品ID",
|
||||
)
|
||||
|
||||
status: Mapped[str] = mapped_column(
|
||||
String(50), nullable=False, default="pending", comment="产品状态",
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), comment="创建时间",
|
||||
)
|
||||
|
||||
# ---- 关系 ----
|
||||
order: Mapped["ProductionOrder"] = relationship("ProductionOrder", lazy="selectin")
|
||||
parent_product: Mapped["Product | None"] = relationship(
|
||||
"Product", remote_side="Product.id", lazy="selectin",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Product {self.serial_number}>"
|
||||
Reference in New Issue
Block a user