在删除用户前,先删除该用户及其子账号拥有的所有 bots(agent_bots 表的 ON DELETE CASCADE 会自动清理关联的

agent_mcp_servers、agent_chat_sessions、bot_shares 记录),然后再删除用户。
This commit is contained in:
朱潮 2026-04-13 16:52:52 +08:00
parent 85bd60cc7e
commit e753127889

View File

@ -3392,6 +3392,13 @@ async def delete_user(user_id: str, authorization: Optional[str] = Header(None))
async with pool.connection() as conn: async with pool.connection() as conn:
async with conn.cursor() as cursor: async with conn.cursor() as cursor:
# 先删除该用户及其子账号拥有的所有 bots级联清理 mcp_servers, chat_sessions, shares
await cursor.execute(
"DELETE FROM agent_bots WHERE owner_id = %s OR owner_id IN (SELECT id FROM agent_user WHERE parent_id = %s)",
(user_id, user_id)
)
# 再删除用户(子账号通过 parent_id ON DELETE CASCADE 自动删除)
await cursor.execute("DELETE FROM agent_user WHERE id = %s RETURNING username", (user_id,)) await cursor.execute("DELETE FROM agent_user WHERE id = %s RETURNING username", (user_id,))
row = await cursor.fetchone() row = await cursor.fetchone()