diff --git a/backend/app/admin/model/sys_role_menu.py b/backend/app/admin/model/m2m.py similarity index 57% rename from backend/app/admin/model/sys_role_menu.py rename to backend/app/admin/model/m2m.py index e1f2cb7d..31040390 100644 --- a/backend/app/admin/model/sys_role_menu.py +++ b/backend/app/admin/model/m2m.py @@ -4,6 +4,14 @@ from backend.common.model import MappedBase +sys_user_role = Table( + 'sys_user_role', + MappedBase.metadata, + Column('id', INT, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'), + Column('user_id', Integer, ForeignKey('sys_user.id', ondelete='CASCADE'), primary_key=True, comment='用户ID'), + Column('role_id', Integer, ForeignKey('sys_role.id', ondelete='CASCADE'), primary_key=True, comment='角色ID'), +) + sys_role_menu = Table( 'sys_role_menu', MappedBase.metadata, diff --git a/backend/app/admin/model/sys_dept.py b/backend/app/admin/model/sys_dept.py index 2e1492e9..a85805d9 100644 --- a/backend/app/admin/model/sys_dept.py +++ b/backend/app/admin/model/sys_dept.py @@ -22,11 +22,13 @@ class Dept(Base): email: Mapped[str | None] = mapped_column(String(50), default=None, comment='邮箱') status: Mapped[int] = mapped_column(default=1, comment='部门状态(0停用 1正常)') del_flag: Mapped[bool] = mapped_column(default=False, comment='删除标志(0删除 1存在)') + # 父级部门一对多 parent_id: Mapped[int | None] = mapped_column( ForeignKey('sys_dept.id', ondelete='SET NULL'), default=None, index=True, comment='父部门ID' ) parent: Mapped[Union['Dept', None]] = relationship(init=False, back_populates='children', remote_side=[id]) children: Mapped[list['Dept'] | None] = relationship(init=False, back_populates='parent') + # 部门用户一对多 users: Mapped[list['User']] = relationship(init=False, back_populates='dept') # noqa: F821 diff --git a/backend/app/admin/model/sys_dict_data.py b/backend/app/admin/model/sys_dict_data.py index 884bdc6b..4eac82b6 100644 --- a/backend/app/admin/model/sys_dict_data.py +++ b/backend/app/admin/model/sys_dict_data.py @@ -18,6 +18,9 @@ class DictData(Base): sort: Mapped[int] = mapped_column(default=0, comment='排序') status: Mapped[int] = mapped_column(default=1, comment='状态(0停用 1正常)') remark: Mapped[str | None] = mapped_column(LONGTEXT, default=None, comment='备注') + # 字典类型一对多 - type_id: Mapped[int] = mapped_column(ForeignKey('sys_dict_type.id'), default=0, comment='字典类型关联ID') + type_id: Mapped[int] = mapped_column( + ForeignKey('sys_dict_type.id', ondelete='CASCADE'), default=0, comment='字典类型关联ID' + ) type: Mapped['DictType'] = relationship(init=False, back_populates='datas') # noqa: F821 diff --git a/backend/app/admin/model/sys_dict_type.py b/backend/app/admin/model/sys_dict_type.py index 3190162e..c5676b4a 100644 --- a/backend/app/admin/model/sys_dict_type.py +++ b/backend/app/admin/model/sys_dict_type.py @@ -17,5 +17,6 @@ class DictType(Base): code: Mapped[str] = mapped_column(String(32), unique=True, comment='字典类型编码') status: Mapped[int] = mapped_column(default=1, comment='状态(0停用 1正常)') remark: Mapped[str | None] = mapped_column(LONGTEXT, default=None, comment='备注') + # 字典类型一对多 datas: Mapped[list['DictData']] = relationship(init=False, back_populates='type') # noqa: F821 diff --git a/backend/app/admin/model/sys_menu.py b/backend/app/admin/model/sys_menu.py index f3233b2c..8b75d29e 100644 --- a/backend/app/admin/model/sys_menu.py +++ b/backend/app/admin/model/sys_menu.py @@ -6,7 +6,7 @@ from sqlalchemy.dialects.mysql import LONGTEXT from sqlalchemy.orm import Mapped, mapped_column, relationship -from backend.app.admin.model.sys_role_menu import sys_role_menu +from backend.app.admin.model.m2m import sys_role_menu from backend.common.model import Base, id_key @@ -29,13 +29,13 @@ class Menu(Base): show: Mapped[int] = mapped_column(default=1, comment='是否显示(0否 1是)') cache: Mapped[int] = mapped_column(default=1, comment='是否缓存(0否 1是)') remark: Mapped[str | None] = mapped_column(LONGTEXT, default=None, comment='备注') + # 父级菜单一对多 parent_id: Mapped[int | None] = mapped_column( ForeignKey('sys_menu.id', ondelete='SET NULL'), default=None, index=True, comment='父菜单ID' ) parent: Mapped[Union['Menu', None]] = relationship(init=False, back_populates='children', remote_side=[id]) children: Mapped[list['Menu'] | None] = relationship(init=False, back_populates='parent') + # 菜单角色多对多 - roles: Mapped[list['Role']] = relationship( # noqa: F821 - init=False, secondary=sys_role_menu, back_populates='menus' - ) + roles: Mapped[list['Role']] = relationship(init=False, secondary=sys_role_menu, back_populates='menus') # noqa: F821 diff --git a/backend/app/admin/model/sys_role.py b/backend/app/admin/model/sys_role.py index acf46c01..415329f1 100644 --- a/backend/app/admin/model/sys_role.py +++ b/backend/app/admin/model/sys_role.py @@ -4,8 +4,7 @@ from sqlalchemy.dialects.mysql import LONGTEXT from sqlalchemy.orm import Mapped, mapped_column, relationship -from backend.app.admin.model.sys_role_menu import sys_role_menu -from backend.app.admin.model.sys_user_role import sys_user_role +from backend.app.admin.model.m2m import sys_role_menu, sys_user_role from backend.common.model import Base, id_key @@ -19,11 +18,9 @@ class Role(Base): data_scope: Mapped[int | None] = mapped_column(default=2, comment='权限范围(1:全部数据权限 2:自定义数据权限)') status: Mapped[int] = mapped_column(default=1, comment='角色状态(0停用 1正常)') remark: Mapped[str | None] = mapped_column(LONGTEXT, default=None, comment='备注') + # 角色用户多对多 - users: Mapped[list['User']] = relationship( # noqa: F821 - init=False, secondary=sys_user_role, back_populates='roles' - ) + users: Mapped[list['User']] = relationship(init=False, secondary=sys_user_role, back_populates='roles') # noqa: F821 + # 角色菜单多对多 - menus: Mapped[list['Menu']] = relationship( # noqa: F821 - init=False, secondary=sys_role_menu, back_populates='roles' - ) + menus: Mapped[list['Menu']] = relationship(init=False, secondary=sys_role_menu, back_populates='roles') # noqa: F821 diff --git a/backend/app/admin/model/sys_user.py b/backend/app/admin/model/sys_user.py index dab80193..5a11d429 100644 --- a/backend/app/admin/model/sys_user.py +++ b/backend/app/admin/model/sys_user.py @@ -6,7 +6,7 @@ from sqlalchemy import ForeignKey, String from sqlalchemy.orm import Mapped, mapped_column, relationship -from backend.app.admin.model.sys_user_role import sys_user_role +from backend.app.admin.model.m2m import sys_user_role from backend.common.model import Base, id_key from backend.database.db_mysql import uuid4_str from backend.utils.timezone import timezone @@ -32,14 +32,15 @@ class User(Base): phone: Mapped[str | None] = mapped_column(String(11), default=None, comment='手机号') join_time: Mapped[datetime] = mapped_column(init=False, default_factory=timezone.now, comment='注册时间') last_login_time: Mapped[datetime | None] = mapped_column(init=False, onupdate=timezone.now, comment='上次登录') + # 部门用户一对多 dept_id: Mapped[int | None] = mapped_column( ForeignKey('sys_dept.id', ondelete='SET NULL'), default=None, comment='部门关联ID' ) dept: Mapped[Union['Dept', None]] = relationship(init=False, back_populates='users') # noqa: F821 - # 用户角色多对多 - roles: Mapped[list['Role']] = relationship( # noqa: F821 - init=False, secondary=sys_user_role, back_populates='users' - ) - # 用户 OAuth2 一对多 + + # 用户社交信息一对多 socials: Mapped[list['UserSocial']] = relationship(init=False, back_populates='user') # noqa: F821 + + # 用户角色多对多 + roles: Mapped[list['Role']] = relationship(init=False, secondary=sys_user_role, back_populates='users') # noqa: F821 diff --git a/backend/app/admin/model/sys_user_role.py b/backend/app/admin/model/sys_user_role.py deleted file mode 100644 index 1870360e..00000000 --- a/backend/app/admin/model/sys_user_role.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -from sqlalchemy import INT, Column, ForeignKey, Integer, Table - -from backend.common.model import MappedBase - -sys_user_role = Table( - 'sys_user_role', - MappedBase.metadata, - Column('id', INT, primary_key=True, unique=True, index=True, autoincrement=True, comment='主键ID'), - Column('user_id', Integer, ForeignKey('sys_user.id', ondelete='CASCADE'), primary_key=True, comment='用户ID'), - Column('role_id', Integer, ForeignKey('sys_role.id', ondelete='CASCADE'), primary_key=True, comment='角色ID'), -) diff --git a/backend/app/admin/model/sys_user_social.py b/backend/app/admin/model/sys_user_social.py index 42c8833a..50439aa6 100644 --- a/backend/app/admin/model/sys_user_social.py +++ b/backend/app/admin/model/sys_user_social.py @@ -20,7 +20,8 @@ class UserSocial(Base): union_id: Mapped[str | None] = mapped_column(String(20), default=None, comment='第三方用户的 union id') scope: Mapped[str | None] = mapped_column(String(120), default=None, comment='第三方用户授予的权限') code: Mapped[str | None] = mapped_column(String(50), default=None, comment='用户的授权 code') - # 用户 OAuth2 一对多 + + # 用户社交信息一对多 user_id: Mapped[int | None] = mapped_column( ForeignKey('sys_user.id', ondelete='SET NULL'), default=None, comment='用户关联ID' )