修复数据库模型外键约束和代码质量问题
- 为User.created_by添加ForeignKey约束,确保数据完整性 - 修复AdjustmentEnum枚举命名(yes/no -> YES/NO) - 统一User模型的relationship配置,使用back_populates替代backref - 移除User.created_projects的级联删除配置,保持与Project模型一致
This commit is contained in:
@@ -6,8 +6,8 @@ from ..config.database import Base
|
||||
|
||||
|
||||
class AdjustmentEnum(str, Enum):
|
||||
yes = "是"
|
||||
no = "否"
|
||||
YES = "是"
|
||||
NO = "否"
|
||||
|
||||
|
||||
class Project(Base):
|
||||
@@ -48,7 +48,7 @@ class Project(Base):
|
||||
|
||||
# 成本控制
|
||||
total_cost_control = Column(Numeric(15, 2), comment="总体成本(控制)")
|
||||
is_adjusted = Column(Enum(AdjustmentEnum), default=AdjustmentEnum.no, index=True, comment="是否调整")
|
||||
is_adjusted = Column(Enum(AdjustmentEnum), default=AdjustmentEnum.NO, index=True, comment="是否调整")
|
||||
labor_cost_control = Column(Numeric(15, 2), comment="人工成本(控制)")
|
||||
labor_cost_planned = Column(Numeric(15, 2), comment="农民工工资(计划)")
|
||||
labor_cost_paid = Column(Numeric(15, 2), comment="农民工工资(实付)")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# backend/src/models/user.py
|
||||
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Enum
|
||||
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Enum, ForeignKey
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
from ..config.database import Base
|
||||
@@ -23,7 +23,7 @@ class User(Base):
|
||||
email = Column(String(100), unique=True, index=True, comment="邮箱")
|
||||
phone = Column(String(20), comment="电话")
|
||||
is_active = Column(Boolean, default=True, index=True, comment="是否激活")
|
||||
created_by = Column(Integer, comment="创建人ID")
|
||||
created_by = Column(Integer, ForeignKey("users.id", ondelete="RESTRICT"), comment="创建人ID")
|
||||
created_at = Column(
|
||||
DateTime,
|
||||
server_default=func.now(),
|
||||
@@ -36,5 +36,6 @@ class User(Base):
|
||||
comment="更新时间",
|
||||
)
|
||||
|
||||
creator = relationship("User", remote_side="User.id", backref="created_users")
|
||||
created_projects = relationship("Project", back_populates="creator", cascade="all, delete-orphan")
|
||||
created_users = relationship("User", back_populates="creator")
|
||||
creator = relationship("User", remote_side="User.id", back_populates="created_users")
|
||||
created_projects = relationship("Project", back_populates="creator")
|
||||
|
||||
Reference in New Issue
Block a user