diff --git a/app/api/routers/rbac_admin.py b/app/api/routers/rbac_admin.py index 9295da4..5454264 100644 --- a/app/api/routers/rbac_admin.py +++ b/app/api/routers/rbac_admin.py @@ -242,15 +242,20 @@ async def delete_user( # 获取数据库中所有表的列信息,避免对没有 user_id 的表执行删除 from sqlalchemy import inspect as sa_inspect - insp = sa_inspect(db.session.bind) - tables_with_user_id = set() - for tname in cascade_tables: - try: - cols = [c["name"] for c in insp.get_columns(tname)] - if "user_id" in cols: - tables_with_user_id.add(tname) - except Exception: - pass + + def _get_tables_with_user_id(sync_conn): + insp = sa_inspect(sync_conn) + result = set() + for tname in cascade_tables: + try: + cols = [c["name"] for c in insp.get_columns(tname)] + if "user_id" in cols: + result.add(tname) + except Exception: + pass + return result + + tables_with_user_id = await db.session.run_sync(_get_tables_with_user_id) deleted_count = 0 for table in tables_with_user_id: