修复数据库模型外键约束和代码质量问题

- 为User.created_by添加ForeignKey约束,确保数据完整性
- 修复AdjustmentEnum枚举命名(yes/no -> YES/NO)
- 统一User模型的relationship配置,使用back_populates替代backref
- 移除User.created_projects的级联删除配置,保持与Project模型一致
This commit is contained in:
xsl
2026-01-26 09:51:13 +08:00
parent e17b29ca41
commit 58a1d50261
2 changed files with 8 additions and 7 deletions
+3 -3
View File
@@ -6,8 +6,8 @@ from ..config.database import Base
class AdjustmentEnum(str, Enum): class AdjustmentEnum(str, Enum):
yes = "" YES = ""
no = "" NO = ""
class Project(Base): class Project(Base):
@@ -48,7 +48,7 @@ class Project(Base):
# 成本控制 # 成本控制
total_cost_control = Column(Numeric(15, 2), comment="总体成本(控制)") 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_control = Column(Numeric(15, 2), comment="人工成本(控制)")
labor_cost_planned = Column(Numeric(15, 2), comment="农民工工资(计划)") labor_cost_planned = Column(Numeric(15, 2), comment="农民工工资(计划)")
labor_cost_paid = Column(Numeric(15, 2), comment="农民工工资(实付)") labor_cost_paid = Column(Numeric(15, 2), comment="农民工工资(实付)")
+5 -4
View File
@@ -1,5 +1,5 @@
# backend/src/models/user.py # 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.sql import func
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from ..config.database import Base from ..config.database import Base
@@ -23,7 +23,7 @@ class User(Base):
email = Column(String(100), unique=True, index=True, comment="邮箱") email = Column(String(100), unique=True, index=True, comment="邮箱")
phone = Column(String(20), comment="电话") phone = Column(String(20), comment="电话")
is_active = Column(Boolean, default=True, index=True, 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( created_at = Column(
DateTime, DateTime,
server_default=func.now(), server_default=func.now(),
@@ -36,5 +36,6 @@ class User(Base):
comment="更新时间", comment="更新时间",
) )
creator = relationship("User", remote_side="User.id", backref="created_users") created_users = relationship("User", back_populates="creator")
created_projects = relationship("Project", back_populates="creator", cascade="all, delete-orphan") creator = relationship("User", remote_side="User.id", back_populates="created_users")
created_projects = relationship("Project", back_populates="creator")