1、后端数据库model优化,解决mysql、sqlite初始化问题,同时满足

2、部门表移除创建人,负责会和user表有外键循环引用问题,这样设计合理
3、前端所有列表左对齐,移除部门详情中创建人字段
This commit is contained in:
zhangtao
2024-12-11 20:20:46 +08:00
parent 0a7d7ebcd6
commit 07583dd3a0
19 changed files with 244 additions and 364 deletions
+3 -33
View File
@@ -22,52 +22,22 @@ class DeptModel(ModelBase):
# 层级关系
parent_id = Column(
Integer,
ForeignKey("system_dept.id", ondelete="SET NULL", onupdate="CASCADE"),
ForeignKey("system_dept.id", ondelete="CASCADE", onupdate="CASCADE"),
nullable=True,
index=True,
comment="父级部门ID"
)
parent = relationship(
"DeptModel",
remote_side=[id],
backref="children",
lazy="selectin",
cascade='all, delete-orphan',
uselist=False
)
# 状态字段
available = Column(Boolean, default=True, nullable=False, comment="是否启用(True:启用 False:禁用)")
# 关联关系
users = relationship(
"UserModel",
back_populates="dept",
foreign_keys="UserModel.dept_id",
lazy="selectin"
)
roles = relationship(
"RoleModel",
secondary="system_role_depts",
back_populates="depts",
lazy="selectin"
)
# 审计字段
description = Column(Text, nullable=True, comment="备注说明")
created_at = Column(DateTime, default=datetime.now, comment='创建时间')
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
creator_id = Column(
Integer,
ForeignKey("system_user.id", ondelete="SET NULL", onupdate="CASCADE"),
nullable=True,
index=True,
comment="创建人ID"
)
creator = relationship(
"UserModel",
foreign_keys=[creator_id],
lazy="joined",
post_update=True,
uselist=False
)
@@ -47,10 +47,8 @@ class MenuModel(ModelBase):
)
parent = relationship(
"MenuModel",
remote_side=[id],
backref="children",
lazy="joined",
post_update=True,
cascade='all, delete-orphan',
primaryjoin="MenuModel.parent_id == MenuModel.id",
uselist=False
)
@@ -23,21 +23,6 @@ class PositionModel(ModelBase):
# 状态字段
available = Column(Boolean, default=True, nullable=False, comment="是否启用(True:启用 False:禁用)")
# 关联关系
dept_id = Column(
Integer,
ForeignKey("system_dept.id", ondelete="SET NULL", onupdate="CASCADE"),
nullable=True,
index=True,
comment="所属部门ID"
)
dept = relationship(
'DeptModel',
primaryjoin="PositionModel.dept_id == DeptModel.id",
lazy="joined",
post_update=True,
uselist=False
)
users = relationship(
"UserModel",
secondary=UserPositionsModel.__tablename__,
@@ -83,6 +83,8 @@ class RoleModel(ModelBase):
"MenuModel",
secondary=RoleMenusModel.__tablename__,
lazy="joined",
cascade="all, delete",
passive_deletes=True,
post_update=True,
uselist=True
)
@@ -90,6 +92,8 @@ class RoleModel(ModelBase):
"DeptModel",
secondary=RoleDeptsModel.__tablename__,
lazy="joined",
cascade="all, delete",
passive_deletes=True,
post_update=True,
uselist=True
)
+11 -14
View File
@@ -80,28 +80,24 @@ class UserModel(ModelBase):
# 组织信息
dept_id = Column(
Integer,
ForeignKey('system_dept.id', ondelete="SET NULL", onupdate="CASCADE"),
nullable=True,
index=True,
comment="所属部门ID"
Integer,
ForeignKey('system_dept.id', ondelete="SET NULL", onupdate="CASCADE"),
nullable=True, index=True, comment="部门ID"
)
dept = relationship(
"DeptModel",
back_populates="users",
foreign_keys=[dept_id],
lazy="joined",
post_update=True
)
'DeptModel',
primaryjoin="UserModel.dept_id == DeptModel.id",
lazy="select",
uselist=False)
roles = relationship(
"RoleModel",
secondary=UserRolesModel.__tablename__,
lazy="selectin",
lazy="joined",
uselist=True)
positions = relationship(
"PositionModel",
secondary=UserPositionsModel.__tablename__,
lazy="selectin",
lazy="joined",
uselist=True)
# 审计字段
@@ -119,5 +115,6 @@ class UserModel(ModelBase):
"UserModel",
remote_side=[id],
foreign_keys=[creator_id],
lazy="selectin"
lazy="selectin",
uselist=False
)
@@ -4,7 +4,7 @@ from typing import Optional
from pydantic import BaseModel, ConfigDict, Field, model_validator
from app.core.base_schema import BaseSchema
from app.core.validator import DateTimeStr
class DeptCreateSchema(BaseModel):
"""部门创建模型"""
@@ -28,8 +28,11 @@ class DeptUpdateSchema(DeptCreateSchema):
id: int = Field(..., gt=0, description="部门ID")
class DeptOutSchema(DeptCreateSchema, BaseSchema):
class DeptOutSchema(DeptCreateSchema):
"""部门响应模型"""
model_config = ConfigDict(from_attributes=True)
parent_name: Optional[str] = Field(default=None, max_length=40, description="父部门名称")
id: int = Field(description="主键ID")
created_at: DateTimeStr = Field(description="创建时间")
updated_at: DateTimeStr = Field(description="更新时间")