From 58a1d50261a1dd2b45ef7d6e5b4aa95459f3bd09 Mon Sep 17 00:00:00 2001 From: xsl Date: Mon, 26 Jan 2026 09:51:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E5=A4=96=E9=94=AE=E7=BA=A6=E6=9D=9F=E5=92=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B4=A8=E9=87=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为User.created_by添加ForeignKey约束,确保数据完整性 - 修复AdjustmentEnum枚举命名(yes/no -> YES/NO) - 统一User模型的relationship配置,使用back_populates替代backref - 移除User.created_projects的级联删除配置,保持与Project模型一致 --- backend/src/models/project.py | 6 +++--- backend/src/models/user.py | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/backend/src/models/project.py b/backend/src/models/project.py index c913e9ab..a2451ae7 100644 --- a/backend/src/models/project.py +++ b/backend/src/models/project.py @@ -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="农民工工资(实付)") diff --git a/backend/src/models/user.py b/backend/src/models/user.py index 71cd2b32..bf186944 100644 --- a/backend/src/models/user.py +++ b/backend/src/models/user.py @@ -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")